diff options
author | Nadrieril | 2019-04-14 23:28:48 +0200 |
---|---|---|
committer | Nadrieril | 2019-04-14 23:28:48 +0200 |
commit | ac2448ea2240db0d3ae8f7d3574ce7ddc2fd57f9 (patch) | |
tree | 9bf5f8324df818a22da4eae0e72989e8b039b64f /improved_slice_patterns/src | |
parent | beadb7a9641dda256e505b6ad13fed5de701b040 (diff) | |
parent | 642d9f4519e0b792fa40879d5692f146afde3442 (diff) |
Merge branch 'publish-improved_slice_patterns'
Diffstat (limited to '')
-rw-r--r-- | improved_slice_patterns/src/lib.rs (renamed from iter_patterns/src/lib.rs) | 163 |
1 files changed, 86 insertions, 77 deletions
diff --git a/iter_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs index 657b538..3e459bb 100644 --- a/iter_patterns/src/lib.rs +++ b/improved_slice_patterns/src/lib.rs @@ -1,27 +1,44 @@ #![feature(slice_patterns)] +#![doc(html_root_url = "https://docs.rs/improved_slice_patterns/1.0.1")] -/* Destructure an iterator using the syntax of slice_patterns. - * Wraps the match body in `Some` if there was a match; returns - * `None` otherwise. - * Contrary to slice_patterns, this allows moving out - * of the iterator. - * A variable length pattern (`x..`) is only allowed as the last - * pattern, unless the iterator is double-ended. - * - * Example: - * ``` - * let vec = vec![Some(1), Some(2), None]; - * - * destructure_iter!(vec.into_iter(); - * [Some(x), y.., z] => { - * // x: usize - * // y: impl Iterator<Option<usize>> - * // z: Option<usize> - * } - * ) - * ``` - * -*/ +//! A tiny crate that provides two macros to help matching +//! on `Vec`s and iterators using the syntax of +//! [`slice_patterns`][slice_patterns] +//! +//! [slice_patterns]: https://doc.rust-lang.org/nightly/unstable-book/language-features/slice-patterns.html + +/// Destructure an iterator using the syntax of slice_patterns. +/// +/// Wraps the match body in `Some` if there was a match; returns +/// `None` otherwise. +/// +/// Contrary to slice_patterns, this allows moving out +/// of the iterator. +/// +/// A variable length pattern (`x..`) is only allowed as the last +/// pattern, unless the iterator is double-ended. +/// +/// Example: +/// ```edition2018 +/// use improved_slice_patterns::destructure_iter; +/// +/// let vec = vec![Some(1), Some(2), Some(3), None]; +/// +/// let res = destructure_iter!(vec.into_iter(); +/// [Some(x), y.., z] => { +/// // x: usize +/// // y: impl Iterator<Option<usize>> +/// // z: Option<usize> +/// (x, y.collect::<Vec<_>>(), z) +/// } +/// ); +/// +/// assert_eq!(res, Some((1, vec![Some(2), Some(3)], None))); +/// +/// # Ok::<(), ()>(()) +/// ``` +/// +/// #[macro_export] macro_rules! destructure_iter { // Variable length pattern @@ -108,29 +125,53 @@ macro_rules! destructure_iter { }; } -/* Pattern-match on a vec using the syntax of slice_patterns. - * Wraps the match body in `Some` if there was a match; returns - * `None` otherwise. - * A variable length pattern (`x..`) returns an iterator. - * - * Example: - * ``` - * let vec = vec![Some(1), Some(2), None]; - * - * match_vec!(vec; - * [Some(x), y.., z] => { - * // x: usize - * // y: impl Iterator<Option<usize>> - * // z: Option<usize> - * } - * [x, Some(0)] => { - * // x: Option<usize> - * }, - * [..] => { } - * ) - * ``` - * -*/ +/// Pattern-match on a vec using the syntax of slice_patterns. +/// +/// Wraps the match body in `Some` if there was a match; returns +/// `None` otherwise. +/// +/// Contrary to slice_patterns, this allows moving out +/// of the `Vec`. +/// +/// A variable length pattern (`x..`) returns an iterator. +/// +/// Example: +/// ```edition2018 +/// #![feature(slice_patterns)] +/// use improved_slice_patterns::match_vec; +/// +/// let vec = vec![Some(1), Some(2), Some(3), None]; +/// +/// let res = match_vec!(vec; +/// [Some(_), y.., None] => { +/// y.collect::<Vec<_>>() +/// }, +/// [None, None] => { +/// vec![] +/// }, +/// [..] => vec![] +/// ); +/// +/// assert_eq!(res, Some(vec![Some(2), Some(3)])); +/// +/// +/// let vec = vec![Some(1), Some(2), Some(3), None]; +/// +/// let res = match_vec!(vec; +/// [Some(_), y.., Some(_)] => { +/// y.collect::<Vec<_>>() +/// }, +/// [None, None] => { +/// vec![] +/// }, +/// ); +/// +/// assert_eq!(res, None); // there was no match +/// +/// # Ok::<(), ()>(()) +/// ``` +/// +/// #[macro_export] macro_rules! match_vec { // Variable length pattern @@ -227,38 +268,6 @@ macro_rules! match_vec { }; } -/* Pattern-match on an iterator using the syntax of slice_patterns. - * Wraps the match body in `Some` if there was a match; returns - * `None` otherwise. - * - * Example: - * ``` - * let vec = vec![Some(1), Some(2), None]; - * - * match_iter!(vec.into_iter(); - * [Some(x), y.., z] => { - * // x: usize - * // y: impl Iterator<Option<usize>> - * // z: Option<usize> - * }, - * [x, Some(0)] => { - * // x: Option<usize> - * }, - * [..] => { - * ) - * ``` - * -*/ -#[macro_export] -macro_rules! match_iter { - ($arg:expr; $($args:tt)*) => { - { - let vec: Vec<_> = $arg.collect(); - $crate::match_vec!(vec; $($args)*) - } - }; -} - #[test] fn test() { let test = |v: Vec<Option<isize>>| { |