diff options
author | Jeff Vander Stoep <jeffv@google.com> | 2020-12-21 16:30:06 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-12-21 16:30:06 +0000 |
commit | 61d5b7b12ce87f98f0cf2aefb90264fc4493f5da (patch) | |
tree | 7ec9327211e7d2dfd920bdb311bd208166913e60 | |
parent | ae94b9e9ba1c1ad88551ea3105fcf48ff6852b13 (diff) | |
parent | 0817547637c001c00dda06e591252dd77fe8ffaf (diff) | |
download | matches-61d5b7b12ce87f98f0cf2aefb90264fc4493f5da.tar.gz |
Initial import of matches v0.1.8 am: 3d4b232b1b am: 621a42bf95 am: 0817547637
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/matches/+/1534338
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: If804fc19228c35767660bdc92fb35d2ddc941a92
-rw-r--r-- | Cargo.toml | 24 | ||||
-rw-r--r-- | Cargo.toml.orig | 12 | ||||
-rw-r--r-- | LICENSE | 25 | ||||
-rw-r--r-- | METADATA | 19 | ||||
-rw-r--r-- | MODULE_LICENSE_MIT | 0 | ||||
-rw-r--r-- | OWNERS | 1 | ||||
-rw-r--r-- | lib.rs | 126 | ||||
-rw-r--r-- | tests/macro_use_one.rs | 11 |
8 files changed, 218 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..030159e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,24 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g. crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "matches" +version = "0.1.8" +authors = ["Simon Sapin <simon.sapin@exyr.org>"] +description = "A macro to evaluate, as a boolean, whether an expression matches a pattern." +documentation = "https://docs.rs/matches/" +license = "MIT" +repository = "https://github.com/SimonSapin/rust-std-candidates" + +[lib] +name = "matches" +path = "lib.rs" diff --git a/Cargo.toml.orig b/Cargo.toml.orig new file mode 100644 index 0000000..afc7422 --- /dev/null +++ b/Cargo.toml.orig @@ -0,0 +1,12 @@ +[package] +name = "matches" +version = "0.1.8" +authors = ["Simon Sapin <simon.sapin@exyr.org>"] +license = "MIT" +repository = "https://github.com/SimonSapin/rust-std-candidates" +description = "A macro to evaluate, as a boolean, whether an expression matches a pattern." +documentation = "https://docs.rs/matches/" + +[lib] +name = "matches" +path = "lib.rs" @@ -0,0 +1,25 @@ +Copyright (c) 2014-2016 Simon Sapin + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/METADATA b/METADATA new file mode 100644 index 0000000..2848b7a --- /dev/null +++ b/METADATA @@ -0,0 +1,19 @@ +name: "matches" +description: "A macro to evaluate, as a boolean, whether an expression matches a pattern." +third_party { + url { + type: HOMEPAGE + value: "https://crates.io/crates/matches" + } + url { + type: ARCHIVE + value: "https://static.crates.io/crates/matches/matches-0.1.8.crate" + } + version: "0.1.8" + license_type: NOTICE + last_upgrade_date { + year: 2020 + month: 12 + day: 15 + } +} diff --git a/MODULE_LICENSE_MIT b/MODULE_LICENSE_MIT new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/MODULE_LICENSE_MIT @@ -0,0 +1 @@ +include platform/prebuilts/rust:/OWNERS @@ -0,0 +1,126 @@ +/// Check if an expression matches a refutable pattern. +/// +/// Syntax: `matches!(` *expression* `,` *pattern* `)` +/// +/// Return a boolean, true if the expression matches the pattern, false otherwise. +/// +/// # Examples +/// +/// ``` +/// #[macro_use] +/// extern crate matches; +/// +/// pub enum Foo<T> { +/// A, +/// B(T), +/// } +/// +/// impl<T> Foo<T> { +/// pub fn is_a(&self) -> bool { +/// matches!(*self, Foo::A) +/// } +/// +/// pub fn is_b(&self) -> bool { +/// matches!(*self, Foo::B(_)) +/// } +/// } +/// +/// # fn main() { } +/// ``` +#[macro_export] +macro_rules! matches { + ($expression:expr, $($pattern:tt)+) => { + match $expression { + $($pattern)+ => true, + _ => false + } + } +} + +/// Assert that an expression matches a refutable pattern. +/// +/// Syntax: `assert_matches!(` *expression* `,` *pattern* `)` +/// +/// Panic with a message that shows the expression if it does not match the +/// pattern. +/// +/// # Examples +/// +/// ``` +/// #[macro_use] +/// extern crate matches; +/// +/// fn main() { +/// let data = [1, 2, 3]; +/// assert_matches!(data.get(1), Some(_)); +/// } +/// ``` +#[macro_export] +macro_rules! assert_matches { + ($expression:expr, $($pattern:tt)+) => { + match $expression { + $($pattern)+ => (), + ref e => panic!("assertion failed: `{:?}` does not match `{}`", e, stringify!($($pattern)+)), + } + } +} + +/// Assert that an expression matches a refutable pattern using debug assertions. +/// +/// Syntax: `debug_assert_matches!(` *expression* `,` *pattern* `)` +/// +/// If debug assertions are enabled, panic with a message that shows the +/// expression if it does not match the pattern. +/// +/// When debug assertions are not enabled, this macro does nothing. +/// +/// # Examples +/// +/// ``` +/// #[macro_use] +/// extern crate matches; +/// +/// fn main() { +/// let data = [1, 2, 3]; +/// debug_assert_matches!(data.get(1), Some(_)); +/// } +/// ``` +#[macro_export] +macro_rules! debug_assert_matches { + ($expression:expr, $($pattern:tt)+) => { + if cfg!(debug_assertions) { + match $expression { + $($pattern)+ => (), + ref e => panic!("assertion failed: `{:?}` does not match `{}`", e, stringify!($($pattern)+)), + } + } + } +} + +#[test] +fn matches_works() { + let foo = Some("-12"); + assert!(matches!(foo, Some(bar) if + matches!(bar.as_bytes()[0], b'+' | b'-') && + matches!(bar.as_bytes()[1], b'0'...b'9') + )); +} + +#[test] +fn assert_matches_works() { + let foo = Some("-12"); + assert_matches!(foo, Some(bar) if + matches!(bar.as_bytes()[0], b'+' | b'-') && + matches!(bar.as_bytes()[1], b'0'...b'9') + ); +} + +#[test] +#[should_panic(expected = "assertion failed: `Some(\"-AB\")` does not match ")] +fn assert_matches_panics() { + let foo = Some("-AB"); + assert_matches!(foo, Some(bar) if + matches!(bar.as_bytes()[0], b'+' | b'-') && + matches!(bar.as_bytes()[1], b'0'...b'9') + ); +} diff --git a/tests/macro_use_one.rs b/tests/macro_use_one.rs new file mode 100644 index 0000000..a527a89 --- /dev/null +++ b/tests/macro_use_one.rs @@ -0,0 +1,11 @@ +// https://github.com/SimonSapin/rust-std-candidates/issues/12 +#[macro_use(matches)] extern crate matches; + +#[test] +fn matches_works() { + let foo = Some("-12"); + assert!(matches!(foo, Some(bar) if + matches!(bar.as_bytes()[0], b'+' | b'-') && + matches!(bar.as_bytes()[1], b'0'...b'9') + )); +} |