summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNadrieril2019-04-20 22:11:08 +0200
committerNadrieril2019-04-20 22:11:08 +0200
commit6bbe7df139552695081af735bd82945e5e22ed05 (patch)
tree3824894a14656da050d311016fdd6bdceb3a5bb7
parent9fbc4f6b9a5496c8e41a079484bc9010b25849df (diff)
improved_slice_patterns now returns ownership on error
-rw-r--r--Cargo.lock5
-rw-r--r--dhall_core/Cargo.toml2
-rw-r--r--dhall_core/src/parser.rs2
-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
6 files changed, 35 insertions, 13 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 097dd89..fc30d65 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -71,6 +71,7 @@ dependencies = [
"bytecount 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dhall_core 0.1.0",
"dhall_generator 0.1.0",
+ "improved_slice_patterns 2.0.0",
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -84,7 +85,7 @@ name = "dhall_core"
version = "0.1.0"
dependencies = [
"dhall_generated_parser 0.1.0",
- "improved_slice_patterns 1.0.1",
+ "improved_slice_patterns 2.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)",
]
@@ -148,7 +149,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "improved_slice_patterns"
-version = "1.0.1"
+version = "2.0.0"
[[package]]
name = "indexmap"
diff --git a/dhall_core/Cargo.toml b/dhall_core/Cargo.toml
index a2aced0..80cf721 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" }
-improved_slice_patterns = { version = "1.0.1", path = "../improved_slice_patterns" }
+improved_slice_patterns = { version = "2.0.0", path = "../improved_slice_patterns" }
diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs
index 051a4e6..31249a9 100644
--- a/dhall_core/src/parser.rs
+++ b/dhall_core/src/parser.rs
@@ -176,7 +176,7 @@ macro_rules! make_parser {
[x..] => Err(
format!("Unexpected children: {:?}", x.collect::<Vec<_>>())
)?,
- ).ok_or_else(|| -> String { unreachable!() })?;
+ ).map_err(|_| -> String { unreachable!() })?;
Ok(ParsedValue::$group(res))
});
(@body,
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();
}