From 0110cdad6f61503d27535c4ae375adccd5b81012 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 22:42:35 +0200 Subject: Remove match_iter from iter_patterns --- iter_patterns/src/lib.rs | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/iter_patterns/src/lib.rs b/iter_patterns/src/lib.rs index 657b538..0672499 100644 --- a/iter_patterns/src/lib.rs +++ b/iter_patterns/src/lib.rs @@ -227,38 +227,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> - * // z: Option - * }, - * [x, Some(0)] => { - * // x: Option - * }, - * [..] => { - * ) - * ``` - * -*/ -#[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>| { -- cgit v1.2.3 From d29f163e03f170a06d700ff0f62a10a489d2f058 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 22:45:22 +0200 Subject: Rename iter_patterns to improved_slice_patterns --- Cargo.lock | 4 +- Cargo.toml | 2 +- dhall_core/Cargo.toml | 2 +- dhall_core/src/parser.rs | 2 +- improved_slice_patterns/Cargo.toml | 9 ++ improved_slice_patterns/src/lib.rs | 261 +++++++++++++++++++++++++++++++++++++ iter_patterns/Cargo.toml | 9 -- iter_patterns/src/lib.rs | 261 ------------------------------------- 8 files changed, 275 insertions(+), 275 deletions(-) create mode 100644 improved_slice_patterns/Cargo.toml create mode 100644 improved_slice_patterns/src/lib.rs delete mode 100644 iter_patterns/Cargo.toml delete mode 100644 iter_patterns/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 6cbb9a7..70298e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,7 +83,7 @@ name = "dhall_core" version = "0.1.0" dependencies = [ "dhall_generated_parser 0.1.0", - "iter_patterns 0.1.0", + "improved_slice_patterns 0.1.0", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "pest 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -146,7 +146,7 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "iter_patterns" +name = "improved_slice_patterns" version = "0.1.0" [[package]] diff --git a/Cargo.toml b/Cargo.toml index fc7d003..0474d20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ members = [ "dhall_generated_parser", "dhall_core", "dhall_generator", - "iter_patterns", + "improved_slice_patterns", ] # # Parser is super slow when not optimized diff --git a/dhall_core/Cargo.toml b/dhall_core/Cargo.toml index a2b242c..bfc418a 100644 --- a/dhall_core/Cargo.toml +++ b/dhall_core/Cargo.toml @@ -12,4 +12,4 @@ doctest = false itertools = "0.8.0" pest = "2.1" dhall_generated_parser = { path = "../dhall_generated_parser" } -iter_patterns = { path = "../iter_patterns" } +improved_slice_patterns = { path = "../improved_slice_patterns" } diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index e520f05..cae6cbd 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -171,7 +171,7 @@ macro_rules! make_parser { #[allow(unused_imports)] use ParsedValue::*; #[allow(unreachable_code)] - let res: $o = iter_patterns::match_vec!($children; + let res: $o = improved_slice_patterns::match_vec!($children; $( [$($args)*] => $body, )* [x..] => Err( format!("Unexpected children: {:?}", x.collect::>()) diff --git a/improved_slice_patterns/Cargo.toml b/improved_slice_patterns/Cargo.toml new file mode 100644 index 0000000..f4d9577 --- /dev/null +++ b/improved_slice_patterns/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "improved_slice_patterns" +version = "0.1.0" +authors = ["Nadrieril "] +license = "BSD-2-Clause" +edition = "2018" + +[lib] +doctest = false diff --git a/improved_slice_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs new file mode 100644 index 0000000..0672499 --- /dev/null +++ b/improved_slice_patterns/src/lib.rs @@ -0,0 +1,261 @@ +#![feature(slice_patterns)] + +/* 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> + * // z: Option + * } + * ) + * ``` + * +*/ +#[macro_export] +macro_rules! destructure_iter { + // Variable length pattern + (@match_forwards, $iter:expr, ($body:expr), $x:ident.., $($rest:tt)*) => { + $crate::destructure_iter!(@match_backwards, + $iter, + ({ + let $x = $iter; + $body + }), + $($rest)* + ) + }; + // Special variable length pattern with a common unary variant + (@match_forwards, $iter:expr, ($body:expr), + $variant:ident ($x:ident).., $($rest:tt)*) => { + $crate::destructure_iter!(@match_backwards, + $iter, + ({ + let $x = $iter + .map(|x| match x { + $variant(y) => y, + _ => unreachable!(), + }); + $body + }), + $($rest)* + ) + }; + // Variable length pattern without a binder + (@match_forwards, $iter:expr, ($body:expr), .., $($rest:tt)*) => { + $crate::destructure_iter!(@match_backwards, + $iter, + ($body), + $($rest)* + ) + }; + // Single item pattern + (@match_forwards, $iter:expr, ($body:expr), $x:pat, $($rest:tt)*) => { + if let std::option::Option::Some($x) = $iter.next() { + $crate::destructure_iter!(@match_forwards, + $iter, + ($body), + $($rest)* + ) + } else { + std::option::Option::None + } + }; + // Single item pattern after a variable length one: declare reversed and take from the end + (@match_backwards, $iter:expr, ($body:expr), $x:pat, $($rest:tt)*) => { + $crate::destructure_iter!(@match_backwards, $iter, ( + if let std::option::Option::Some($x) = $iter.next_back() { + $body + } else { + std::option::Option::None + } + ), $($rest)*) + }; + + // Check no elements remain + (@match_forwards, $iter:expr, ($body:expr) $(,)*) => { + if $iter.next().is_some() { + std::option::Option::None + } else { + $body + } + }; + // After a variable length pattern, everything has already been consumed + (@match_backwards, $iter:expr, ($body:expr) $(,)*) => { + $body + }; + + ($iter:expr; [$($args:tt)*] => $body:expr) => { + { + #[allow(unused_mut)] + let mut iter = $iter; + $crate::destructure_iter!(@match_forwards, + iter, + (std::option::Option::Some($body)), + $($args)*, + ) + } + }; +} + +/* 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> + * // z: Option + * } + * [x, Some(0)] => { + * // x: Option + * }, + * [..] => { } + * ) + * ``` + * +*/ +#[macro_export] +macro_rules! match_vec { + // Variable length pattern + (@make_pat; ($($acc:tt)*), $x:ident.., $($rest:tt)*) => { + $crate::match_vec!(@make_pat; + ($($acc)*, $x..), + $($rest)* + ) + }; + // Special variable length pattern with a common unary variant + (@make_pat; ($($acc:tt)*), $variant:ident ($x:ident).., $($rest:tt)*) => { + $crate::match_vec!(@make_pat; + ($($acc)*, $x..), + $($rest)* + ) + }; + // Variable length pattern without a binder + (@make_pat; ($($acc:tt)*), .., $($rest:tt)*) => { + $crate::match_vec!(@make_pat; + ($($acc)*, ..), + $($rest)* + ) + }; + // Single item pattern + (@make_pat; ($($acc:tt)*), $x:pat, $($rest:tt)*) => { + $crate::match_vec!(@make_pat; + ($($acc)*, $x), + $($rest)* + ) + }; + (@make_pat; (, $($acc:tt)*), $(,)*) => { + [$($acc)*] + }; + (@make_pat; ($($acc:tt)*), $(,)*) => { + [$($acc)*] + }; + + (@make_filter; $x:ident.., $($rest:tt)*) => { + $crate::match_vec!(@make_filter; + $($rest)* + ) + }; + (@make_filter; $variant:ident ($x:ident).., $($rest:tt)*) => { + { + // Circumvent https://github.com/rust-lang/rust/issues/59803 + let is_all_variant = || $x.iter() + .all(|x| match x { + $variant(_) => true, + _ => false, + }); + is_all_variant() + } + && + $crate::match_vec!(@make_filter; + $($rest)* + ) + }; + (@make_filter; .., $($rest:tt)*) => { + $crate::match_vec!(@make_filter; + $($rest)* + ) + }; + (@make_filter; $x:pat, $($rest:tt)*) => { + $crate::match_vec!(@make_filter; + $($rest)* + ) + }; + (@make_filter; $(,)*) => { + true + }; + + ($arg:expr; $( [$($args:tt)*] => $body:expr ),* $(,)*) => { + { + let vec = $arg; + // Match as references to decide which branch to take + // I think `match_default_bindings` should make this always work but + // there may be some patterns this doesn't capture. + #[allow(unused_variables, unreachable_patterns)] + match vec.as_slice() { + $( + $crate::match_vec!(@make_pat; (), $($args)*,) + if + $crate::match_vec!(@make_filter; $($args)*,) + => { + // Actually consume the values + #[allow(unused_mut)] + let mut iter = vec.into_iter(); + $crate::destructure_iter!(iter; [$($args)*] => $body) + } + )* + _ => std::option::Option::None, + } + } + }; +} + +#[test] +fn test() { + let test = |v: Vec>| { + match_vec!(v.into_iter(); + [Some(_x), None, None] => 4, + [Some(_x), None] => 2, + [None, Some(y)] => 1, + [None, _y..] => 3, + [_x.., Some(y), Some(z), None] => y - z, + [Some(ys)..] => ys.sum(), + [] => 0, + [..] => -1, + ) + .unwrap() + }; + + assert_eq!(test(vec![Some(0), None, None]), 4); + assert_eq!(test(vec![Some(0), None]), 2); + assert_eq!(test(vec![None, Some(0)]), 1); + assert_eq!(test(vec![Some(1), Some(2), Some(5), Some(14), None]), -9); + assert_eq!(test(vec![Some(1), Some(2), Some(3), Some(4)]), 10); + assert_eq!(test(vec![None]), 3); + assert_eq!(test(vec![]), 0); + assert_eq!(test(vec![Some(0), None, Some(1)]), -1); + + // Test move out of pattern + struct Foo; + let _: (Foo, Foo) = match_vec!(vec![Some(Foo), Some(Foo)].into_iter(); + [Some(f1), Some(f2)] => (f1, f2), + ) + .unwrap(); +} diff --git a/iter_patterns/Cargo.toml b/iter_patterns/Cargo.toml deleted file mode 100644 index 5a4c45f..0000000 --- a/iter_patterns/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "iter_patterns" -version = "0.1.0" -authors = ["Nadrieril "] -license = "BSD-2-Clause" -edition = "2018" - -[lib] -doctest = false diff --git a/iter_patterns/src/lib.rs b/iter_patterns/src/lib.rs deleted file mode 100644 index 0672499..0000000 --- a/iter_patterns/src/lib.rs +++ /dev/null @@ -1,261 +0,0 @@ -#![feature(slice_patterns)] - -/* 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> - * // z: Option - * } - * ) - * ``` - * -*/ -#[macro_export] -macro_rules! destructure_iter { - // Variable length pattern - (@match_forwards, $iter:expr, ($body:expr), $x:ident.., $($rest:tt)*) => { - $crate::destructure_iter!(@match_backwards, - $iter, - ({ - let $x = $iter; - $body - }), - $($rest)* - ) - }; - // Special variable length pattern with a common unary variant - (@match_forwards, $iter:expr, ($body:expr), - $variant:ident ($x:ident).., $($rest:tt)*) => { - $crate::destructure_iter!(@match_backwards, - $iter, - ({ - let $x = $iter - .map(|x| match x { - $variant(y) => y, - _ => unreachable!(), - }); - $body - }), - $($rest)* - ) - }; - // Variable length pattern without a binder - (@match_forwards, $iter:expr, ($body:expr), .., $($rest:tt)*) => { - $crate::destructure_iter!(@match_backwards, - $iter, - ($body), - $($rest)* - ) - }; - // Single item pattern - (@match_forwards, $iter:expr, ($body:expr), $x:pat, $($rest:tt)*) => { - if let std::option::Option::Some($x) = $iter.next() { - $crate::destructure_iter!(@match_forwards, - $iter, - ($body), - $($rest)* - ) - } else { - std::option::Option::None - } - }; - // Single item pattern after a variable length one: declare reversed and take from the end - (@match_backwards, $iter:expr, ($body:expr), $x:pat, $($rest:tt)*) => { - $crate::destructure_iter!(@match_backwards, $iter, ( - if let std::option::Option::Some($x) = $iter.next_back() { - $body - } else { - std::option::Option::None - } - ), $($rest)*) - }; - - // Check no elements remain - (@match_forwards, $iter:expr, ($body:expr) $(,)*) => { - if $iter.next().is_some() { - std::option::Option::None - } else { - $body - } - }; - // After a variable length pattern, everything has already been consumed - (@match_backwards, $iter:expr, ($body:expr) $(,)*) => { - $body - }; - - ($iter:expr; [$($args:tt)*] => $body:expr) => { - { - #[allow(unused_mut)] - let mut iter = $iter; - $crate::destructure_iter!(@match_forwards, - iter, - (std::option::Option::Some($body)), - $($args)*, - ) - } - }; -} - -/* 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> - * // z: Option - * } - * [x, Some(0)] => { - * // x: Option - * }, - * [..] => { } - * ) - * ``` - * -*/ -#[macro_export] -macro_rules! match_vec { - // Variable length pattern - (@make_pat; ($($acc:tt)*), $x:ident.., $($rest:tt)*) => { - $crate::match_vec!(@make_pat; - ($($acc)*, $x..), - $($rest)* - ) - }; - // Special variable length pattern with a common unary variant - (@make_pat; ($($acc:tt)*), $variant:ident ($x:ident).., $($rest:tt)*) => { - $crate::match_vec!(@make_pat; - ($($acc)*, $x..), - $($rest)* - ) - }; - // Variable length pattern without a binder - (@make_pat; ($($acc:tt)*), .., $($rest:tt)*) => { - $crate::match_vec!(@make_pat; - ($($acc)*, ..), - $($rest)* - ) - }; - // Single item pattern - (@make_pat; ($($acc:tt)*), $x:pat, $($rest:tt)*) => { - $crate::match_vec!(@make_pat; - ($($acc)*, $x), - $($rest)* - ) - }; - (@make_pat; (, $($acc:tt)*), $(,)*) => { - [$($acc)*] - }; - (@make_pat; ($($acc:tt)*), $(,)*) => { - [$($acc)*] - }; - - (@make_filter; $x:ident.., $($rest:tt)*) => { - $crate::match_vec!(@make_filter; - $($rest)* - ) - }; - (@make_filter; $variant:ident ($x:ident).., $($rest:tt)*) => { - { - // Circumvent https://github.com/rust-lang/rust/issues/59803 - let is_all_variant = || $x.iter() - .all(|x| match x { - $variant(_) => true, - _ => false, - }); - is_all_variant() - } - && - $crate::match_vec!(@make_filter; - $($rest)* - ) - }; - (@make_filter; .., $($rest:tt)*) => { - $crate::match_vec!(@make_filter; - $($rest)* - ) - }; - (@make_filter; $x:pat, $($rest:tt)*) => { - $crate::match_vec!(@make_filter; - $($rest)* - ) - }; - (@make_filter; $(,)*) => { - true - }; - - ($arg:expr; $( [$($args:tt)*] => $body:expr ),* $(,)*) => { - { - let vec = $arg; - // Match as references to decide which branch to take - // I think `match_default_bindings` should make this always work but - // there may be some patterns this doesn't capture. - #[allow(unused_variables, unreachable_patterns)] - match vec.as_slice() { - $( - $crate::match_vec!(@make_pat; (), $($args)*,) - if - $crate::match_vec!(@make_filter; $($args)*,) - => { - // Actually consume the values - #[allow(unused_mut)] - let mut iter = vec.into_iter(); - $crate::destructure_iter!(iter; [$($args)*] => $body) - } - )* - _ => std::option::Option::None, - } - } - }; -} - -#[test] -fn test() { - let test = |v: Vec>| { - match_vec!(v.into_iter(); - [Some(_x), None, None] => 4, - [Some(_x), None] => 2, - [None, Some(y)] => 1, - [None, _y..] => 3, - [_x.., Some(y), Some(z), None] => y - z, - [Some(ys)..] => ys.sum(), - [] => 0, - [..] => -1, - ) - .unwrap() - }; - - assert_eq!(test(vec![Some(0), None, None]), 4); - assert_eq!(test(vec![Some(0), None]), 2); - assert_eq!(test(vec![None, Some(0)]), 1); - assert_eq!(test(vec![Some(1), Some(2), Some(5), Some(14), None]), -9); - assert_eq!(test(vec![Some(1), Some(2), Some(3), Some(4)]), 10); - assert_eq!(test(vec![None]), 3); - assert_eq!(test(vec![]), 0); - assert_eq!(test(vec![Some(0), None, Some(1)]), -1); - - // Test move out of pattern - struct Foo; - let _: (Foo, Foo) = match_vec!(vec![Some(Foo), Some(Foo)].into_iter(); - [Some(f1), Some(f2)] => (f1, f2), - ) - .unwrap(); -} -- cgit v1.2.3 From 2e78bf6f5d7cf315aa1cc4fb7f828a6ee1f66b3f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 22:48:53 +0200 Subject: improved_slice_patterns: use doc comments --- improved_slice_patterns/src/lib.rs | 90 +++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/improved_slice_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs index 0672499..1842599 100644 --- a/improved_slice_patterns/src/lib.rs +++ b/improved_slice_patterns/src/lib.rs @@ -1,27 +1,27 @@ #![feature(slice_patterns)] -/* 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> - * // z: Option - * } - * ) - * ``` - * -*/ +/// 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> +/// // z: Option +/// } +/// ) +/// ``` +/// +/// #[macro_export] macro_rules! destructure_iter { // Variable length pattern @@ -108,29 +108,29 @@ 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> - * // z: Option - * } - * [x, Some(0)] => { - * // x: Option - * }, - * [..] => { } - * ) - * ``` - * -*/ +/// 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> +/// // z: Option +/// } +/// [x, Some(0)] => { +/// // x: Option +/// }, +/// [..] => { } +/// ) +/// ``` +/// +/// #[macro_export] macro_rules! match_vec { // Variable length pattern -- cgit v1.2.3 From 2954e502abb274b87ae3dbbb80ef523ebe602b28 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 23:00:01 +0200 Subject: improved_slice_patterns: doctests --- improved_slice_patterns/Cargo.toml | 3 --- improved_slice_patterns/src/lib.rs | 40 +++++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/improved_slice_patterns/Cargo.toml b/improved_slice_patterns/Cargo.toml index f4d9577..f2b7d8e 100644 --- a/improved_slice_patterns/Cargo.toml +++ b/improved_slice_patterns/Cargo.toml @@ -4,6 +4,3 @@ version = "0.1.0" authors = ["Nadrieril "] license = "BSD-2-Clause" edition = "2018" - -[lib] -doctest = false diff --git a/improved_slice_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs index 1842599..726b57a 100644 --- a/improved_slice_patterns/src/lib.rs +++ b/improved_slice_patterns/src/lib.rs @@ -10,15 +10,22 @@ /// /// Example: /// ``` -/// let vec = vec![Some(1), Some(2), None]; +/// use improved_slice_patterns::destructure_iter; /// -/// destructure_iter!(vec.into_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> /// // z: Option +/// (x, y.collect::>(), z) /// } -/// ) +/// ); +/// +/// assert_eq!(res, Some((1, vec![Some(2), Some(3)], None))); +/// +/// # Ok::<(), ()>(()) /// ``` /// /// @@ -115,19 +122,24 @@ macro_rules! destructure_iter { /// /// Example: /// ``` -/// let vec = vec![Some(1), Some(2), None]; +/// #![feature(slice_patterns)] +/// use improved_slice_patterns::match_vec; /// -/// match_vec!(vec; -/// [Some(x), y.., z] => { -/// // x: usize -/// // y: impl Iterator> -/// // z: Option -/// } -/// [x, Some(0)] => { -/// // x: Option +/// let vec = vec![Some(1), Some(2), Some(3), None]; +/// +/// let res = match_vec!(vec; +/// [Some(_), y.., None] => { +/// y.collect::>() /// }, -/// [..] => { } -/// ) +/// [None, None] => { +/// vec![] +/// }, +/// [..] => vec![] +/// ); +/// +/// assert_eq!(res, Some(vec![Some(2), Some(3)])); +/// +/// # Ok::<(), ()>(()) /// ``` /// /// -- cgit v1.2.3 From 6b60e0ded951faa6aff5091300307f5e4b139d8e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 23:05:52 +0200 Subject: improved_slice_patterns: Improve docs --- improved_slice_patterns/src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/improved_slice_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs index 726b57a..56547f9 100644 --- a/improved_slice_patterns/src/lib.rs +++ b/improved_slice_patterns/src/lib.rs @@ -1,5 +1,10 @@ #![feature(slice_patterns)] +//! A tiny crate that provides two macros to help matching +//! on `Vec`s and iterators using [`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. @@ -118,6 +123,8 @@ 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. +/// Contrary to slice_patterns, this allows moving out +/// of the `Vec`. /// A variable length pattern (`x..`) returns an iterator. /// /// Example: @@ -139,6 +146,20 @@ macro_rules! destructure_iter { /// /// 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::>() +/// }, +/// [None, None] => { +/// vec![] +/// }, +/// ); +/// +/// assert_eq!(res, None); // there was no match +/// /// # Ok::<(), ()>(()) /// ``` /// -- cgit v1.2.3 From fa7c760d1058c750967ae091023a54571879b092 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 23:06:44 +0200 Subject: improved_slice_patterns: relicense --- improved_slice_patterns/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/improved_slice_patterns/Cargo.toml b/improved_slice_patterns/Cargo.toml index f2b7d8e..10a43e8 100644 --- a/improved_slice_patterns/Cargo.toml +++ b/improved_slice_patterns/Cargo.toml @@ -2,5 +2,5 @@ name = "improved_slice_patterns" version = "0.1.0" authors = ["Nadrieril "] -license = "BSD-2-Clause" +license = "MIT OR Apache-2.0" edition = "2018" -- cgit v1.2.3 From e5bc3586fc83768a72260fa800bf86be827b9519 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 23:08:45 +0200 Subject: improved_slice_patterns: add README --- improved_slice_patterns/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 improved_slice_patterns/README.md diff --git a/improved_slice_patterns/README.md b/improved_slice_patterns/README.md new file mode 100644 index 0000000..0ad159c --- /dev/null +++ b/improved_slice_patterns/README.md @@ -0,0 +1,23 @@ +# `improved_slice_patterns` + +A tiny crate that provides two macros to help matching +on `Vec`s and iterators using [`slice_patterns`][slice_patterns] + +[slice_patterns]: https://doc.rust-lang.org/nightly/unstable-book/language-features/slice-patterns.html + +## License + +Licensed under either of + + * Apache License, Version 2.0 + (http://www.apache.org/licenses/LICENSE-2.0) + * MIT license + (http://opensource.org/licenses/MIT) + +at your option. + +## Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions -- cgit v1.2.3 From 74e59290247a7201c17dd748b7f554716ad16691 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 23:14:45 +0200 Subject: improved_slice_patterns: prepare for publishing --- Cargo.lock | 4 ++-- improved_slice_patterns/Cargo.toml | 5 ++++- improved_slice_patterns/src/lib.rs | 7 ++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 70298e8..dae9b3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,7 +83,7 @@ name = "dhall_core" version = "0.1.0" dependencies = [ "dhall_generated_parser 0.1.0", - "improved_slice_patterns 0.1.0", + "improved_slice_patterns 1.0.0", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "pest 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -147,7 +147,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "improved_slice_patterns" -version = "0.1.0" +version = "1.0.0" [[package]] name = "itertools" diff --git a/improved_slice_patterns/Cargo.toml b/improved_slice_patterns/Cargo.toml index 10a43e8..7bc6ca0 100644 --- a/improved_slice_patterns/Cargo.toml +++ b/improved_slice_patterns/Cargo.toml @@ -1,6 +1,9 @@ [package] name = "improved_slice_patterns" -version = "0.1.0" +version = "1.0.0" authors = ["Nadrieril "] license = "MIT OR Apache-2.0" edition = "2018" +description = "A tiny crate that provides macros to help matching on Vecs and iterators using the syntax of slice_patterns" +repository = "https://github.com/Nadrieril/dhall-rust" +readme = "README.md" diff --git a/improved_slice_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs index 56547f9..3fdd9c9 100644 --- a/improved_slice_patterns/src/lib.rs +++ b/improved_slice_patterns/src/lib.rs @@ -1,7 +1,8 @@ #![feature(slice_patterns)] //! A tiny crate that provides two macros to help matching -//! on `Vec`s and iterators using [`slice_patterns`][slice_patterns] +//! 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 @@ -14,7 +15,7 @@ /// 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]; @@ -128,7 +129,7 @@ macro_rules! destructure_iter { /// A variable length pattern (`x..`) returns an iterator. /// /// Example: -/// ``` +/// ```edition2018 /// #![feature(slice_patterns)] /// use improved_slice_patterns::match_vec; /// -- cgit v1.2.3 From ee3f9d270bcdbb06ad11daa61667c694196d46f8 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 23:26:03 +0200 Subject: improved_slice_patterns: various tweaks for crates.io --- improved_slice_patterns/Cargo.toml | 5 +++-- improved_slice_patterns/src/lib.rs | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/improved_slice_patterns/Cargo.toml b/improved_slice_patterns/Cargo.toml index 7bc6ca0..9c99e0d 100644 --- a/improved_slice_patterns/Cargo.toml +++ b/improved_slice_patterns/Cargo.toml @@ -1,9 +1,10 @@ [package] name = "improved_slice_patterns" -version = "1.0.0" +version = "1.0.1" # remember to update html_root_url authors = ["Nadrieril "] license = "MIT OR Apache-2.0" edition = "2018" description = "A tiny crate that provides macros to help matching on Vecs and iterators using the syntax of slice_patterns" -repository = "https://github.com/Nadrieril/dhall-rust" readme = "README.md" +repository = "https://github.com/Nadrieril/dhall-rust" +documentation = "https://docs.rs/improved_slice_patterns/" diff --git a/improved_slice_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs index 3fdd9c9..3e459bb 100644 --- a/improved_slice_patterns/src/lib.rs +++ b/improved_slice_patterns/src/lib.rs @@ -1,4 +1,5 @@ #![feature(slice_patterns)] +#![doc(html_root_url = "https://docs.rs/improved_slice_patterns/1.0.1")] //! A tiny crate that provides two macros to help matching //! on `Vec`s and iterators using the syntax of @@ -7,10 +8,13 @@ //! [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. /// @@ -122,10 +126,13 @@ 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. +/// /// Contrary to slice_patterns, this allows moving out /// of the `Vec`. +/// /// A variable length pattern (`x..`) returns an iterator. /// /// Example: -- cgit v1.2.3 From b5105da28ba3c053e3bc0e0631d677d4754d14fb Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 23:27:33 +0200 Subject: improved_slice_patterns: publish new version --- Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dae9b3e..448865e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,7 +83,7 @@ name = "dhall_core" version = "0.1.0" dependencies = [ "dhall_generated_parser 0.1.0", - "improved_slice_patterns 1.0.0", + "improved_slice_patterns 1.0.1", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "pest 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -147,7 +147,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "improved_slice_patterns" -version = "1.0.0" +version = "1.0.1" [[package]] name = "itertools" -- cgit v1.2.3 From 642d9f4519e0b792fa40879d5692f146afde3442 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Apr 2019 23:27:45 +0200 Subject: improved_slice_patterns: README wording --- improved_slice_patterns/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/improved_slice_patterns/README.md b/improved_slice_patterns/README.md index 0ad159c..ab8fd7e 100644 --- a/improved_slice_patterns/README.md +++ b/improved_slice_patterns/README.md @@ -1,7 +1,7 @@ # `improved_slice_patterns` A tiny crate that provides two macros to help matching -on `Vec`s and iterators using [`slice_patterns`][slice_patterns] +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 -- cgit v1.2.3