diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/mac/src/matches.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/mac/src/matches.rs')
-rw-r--r-- | vendor/mac/src/matches.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/vendor/mac/src/matches.rs b/vendor/mac/src/matches.rs new file mode 100644 index 000000000..a788fe478 --- /dev/null +++ b/vendor/mac/src/matches.rs @@ -0,0 +1,44 @@ +//! Pattern Matching macros. + +/// Returns true if an expression matches a pattern. +/// +/// # Example +/// +/// ``` +/// # #[macro_use] extern crate mac; +/// +/// # fn main() { +/// assert!(matches!(2, 1 | 2 | 3)); +/// assert!(matches!('x', 'a' ... 'z')); +/// assert!(!matches!(Some(1), None)); +/// assert!(matches!(Some(42), Some(n) if n == 42)); +/// # } +/// ``` +#[macro_export] +macro_rules! matches { + ($expr:expr, $($pat:tt)+) => { + _tt_as_expr_hack! { + match $expr { + $($pat)+ => true, + _ => false + } + } + } +} + +/// Work around "error: unexpected token: `an interpolated tt`", whatever that +/// means. (Probably rust-lang/rust#22819.) +#[doc(hidden)] +#[macro_export] +macro_rules! _tt_as_expr_hack { + ($value:expr) => ($value) +} + +#[test] +fn test_matches() { + 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') + )); +} |