summaryrefslogtreecommitdiff
path: root/improved_slice_patterns
diff options
context:
space:
mode:
authorNadrieril2019-04-20 23:13:15 +0200
committerNadrieril2019-04-20 23:13:15 +0200
commitb31690a87e2963d4093210a2c58735a2095e651d (patch)
treed6bff16ce18dda443aa8b9a2fb31c31f0a502ec4 /improved_slice_patterns
parent9c8ba84fa5d0b392f19e9e9b8569ee2fbe96bd28 (diff)
parent83bc67d4572fe7961842f915d5559ee489e13dfd (diff)
Merge branch 'whnf'
Massive normalization rewrite, for greatly improved flexibility and performance. Closes #84
Diffstat (limited to 'improved_slice_patterns')
-rw-r--r--improved_slice_patterns/Cargo.toml2
-rw-r--r--improved_slice_patterns/README.md7
-rw-r--r--improved_slice_patterns/src/lib.rs30
3 files changed, 30 insertions, 9 deletions
diff --git a/improved_slice_patterns/Cargo.toml b/improved_slice_patterns/Cargo.toml
index 9c99e0d..a5815e8 100644
--- a/improved_slice_patterns/Cargo.toml
+++ b/improved_slice_patterns/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "improved_slice_patterns"
-version = "1.0.1" # remember to update html_root_url
+version = "2.0.0" # remember to update html_root_url
authors = ["Nadrieril <nadrieril@users.noreply.github.com>"]
license = "MIT OR Apache-2.0"
edition = "2018"
diff --git a/improved_slice_patterns/README.md b/improved_slice_patterns/README.md
index ab8fd7e..7f07530 100644
--- a/improved_slice_patterns/README.md
+++ b/improved_slice_patterns/README.md
@@ -5,6 +5,13 @@ 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
+## Changelog
+
+### V2.0
+
+- `match_vec` now returns a `Result` instead of an `Option`. The `Err` variant
+is used to return ownership of the passed vec if there was no match.
+
## License
Licensed under either of
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();
}