summaryrefslogtreecommitdiff
path: root/improved_slice_patterns
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--improved_slice_patterns/Cargo.toml10
-rw-r--r--improved_slice_patterns/README.md23
-rw-r--r--improved_slice_patterns/src/lib.rs (renamed from iter_patterns/src/lib.rs)163
3 files changed, 119 insertions, 77 deletions
diff --git a/improved_slice_patterns/Cargo.toml b/improved_slice_patterns/Cargo.toml
new file mode 100644
index 0000000..9c99e0d
--- /dev/null
+++ b/improved_slice_patterns/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "improved_slice_patterns"
+version = "1.0.1" # remember to update html_root_url
+authors = ["Nadrieril <nadrieril@users.noreply.github.com>"]
+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"
+readme = "README.md"
+repository = "https://github.com/Nadrieril/dhall-rust"
+documentation = "https://docs.rs/improved_slice_patterns/"
diff --git a/improved_slice_patterns/README.md b/improved_slice_patterns/README.md
new file mode 100644
index 0000000..ab8fd7e
--- /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 the syntax of [`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
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>>| {