diff options
author | Nadrieril | 2019-04-20 23:13:15 +0200 |
---|---|---|
committer | Nadrieril | 2019-04-20 23:13:15 +0200 |
commit | b31690a87e2963d4093210a2c58735a2095e651d (patch) | |
tree | d6bff16ce18dda443aa8b9a2fb31c31f0a502ec4 /improved_slice_patterns/src | |
parent | 9c8ba84fa5d0b392f19e9e9b8569ee2fbe96bd28 (diff) | |
parent | 83bc67d4572fe7961842f915d5559ee489e13dfd (diff) |
Merge branch 'whnf'
Massive normalization rewrite, for greatly improved flexibility
and performance.
Closes #84
Diffstat (limited to 'improved_slice_patterns/src')
-rw-r--r-- | improved_slice_patterns/src/lib.rs | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/improved_slice_patterns/src/lib.rs b/improved_slice_patterns/src/lib.rs index 3e459bb..5478c1b 100644 --- a/improved_slice_patterns/src/lib.rs +++ b/improved_slice_patterns/src/lib.rs @@ -1,5 +1,5 @@ #![feature(slice_patterns)] -#![doc(html_root_url = "https://docs.rs/improved_slice_patterns/1.0.1")] +#![doc(html_root_url = "https://docs.rs/improved_slice_patterns/2.0.0")] //! A tiny crate that provides two macros to help matching //! on `Vec`s and iterators using the syntax of @@ -127,8 +127,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. +/// Wraps the match body in `Ok` if there was a match; returns +/// an `Err` containing the ownership of the vec otherwise. /// /// Contrary to slice_patterns, this allows moving out /// of the `Vec`. @@ -152,7 +152,7 @@ macro_rules! destructure_iter { /// [..] => vec![] /// ); /// -/// assert_eq!(res, Some(vec![Some(2), Some(3)])); +/// assert_eq!(res, Ok(vec![Some(2), Some(3)])); /// /// /// let vec = vec![Some(1), Some(2), Some(3), None]; @@ -166,7 +166,7 @@ macro_rules! destructure_iter { /// }, /// ); /// -/// assert_eq!(res, None); // there was no match +/// assert!(res.is_err()); // there was no match /// /// # Ok::<(), ()>(()) /// ``` @@ -259,10 +259,17 @@ macro_rules! match_vec { // Actually consume the values #[allow(unused_mut)] let mut iter = vec.into_iter(); - $crate::destructure_iter!(iter; [$($args)*] => $body) + let ret = + $crate::destructure_iter!(iter; + [$($args)*] => $body + ); + match ret { + Some(x) => Ok(x), + None => unreachable!(), // Hopefully + } } )* - _ => std::option::Option::None, + _ => std::result::Result::Err(vec), } } }; @@ -294,9 +301,16 @@ fn test() { assert_eq!(test(vec![Some(0), None, Some(1)]), -1); // Test move out of pattern + #[derive(Debug)] struct Foo; - let _: (Foo, Foo) = match_vec!(vec![Some(Foo), Some(Foo)].into_iter(); + let _: (Foo, Foo) = match_vec!(vec![Some(Foo), Some(Foo)]; [Some(f1), Some(f2)] => (f1, f2), ) .unwrap(); + + // Test return ownership if no match + let _: Vec<Foo> = match_vec!(vec![Foo]; + [] => "Error".to_string(), + ) + .unwrap_err(); } |