summaryrefslogtreecommitdiffstats
path: root/third_party/rust/matches
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/matches')
-rw-r--r--third_party/rust/matches/.cargo-checksum.json1
-rw-r--r--third_party/rust/matches/.cargo-ok0
-rw-r--r--third_party/rust/matches/Cargo.toml12
-rw-r--r--third_party/rust/matches/lib.rs62
4 files changed, 75 insertions, 0 deletions
diff --git a/third_party/rust/matches/.cargo-checksum.json b/third_party/rust/matches/.cargo-checksum.json
new file mode 100644
index 000000000..4f1624f6e
--- /dev/null
+++ b/third_party/rust/matches/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{".cargo-ok":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855","Cargo.toml":"a55cb9efcb3820aa9fcdff157bffaa287988ea9d4cec33e638fa7fdd978c7742","lib.rs":"d927e99fc4fd88b0e30b44580384b411e63bc504e9953ac7cc85029437372266"},"package":"bcc3ad8109fa4b522f9b0cd81440422781f564aaf8c195de6b9d6642177ad0dd"} \ No newline at end of file
diff --git a/third_party/rust/matches/.cargo-ok b/third_party/rust/matches/.cargo-ok
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/third_party/rust/matches/.cargo-ok
diff --git a/third_party/rust/matches/Cargo.toml b/third_party/rust/matches/Cargo.toml
new file mode 100644
index 000000000..042e1cc9a
--- /dev/null
+++ b/third_party/rust/matches/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "matches"
+version = "0.1.3"
+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."
+
+[lib]
+name = "matches"
+path = "lib.rs"
+doctest = false
diff --git a/third_party/rust/matches/lib.rs b/third_party/rust/matches/lib.rs
new file mode 100644
index 000000000..eeba978cf
--- /dev/null
+++ b/third_party/rust/matches/lib.rs
@@ -0,0 +1,62 @@
+#[macro_export]
+macro_rules! matches {
+ ($expression:expr, $($pattern:tt)+) => {
+ _matches_tt_as_expr_hack! {
+ match $expression {
+ $($pattern)+ => true,
+ _ => false
+ }
+ }
+ }
+}
+
+/// Work around "error: unexpected token: `an interpolated tt`", whatever that means.
+#[macro_export]
+macro_rules! _matches_tt_as_expr_hack {
+ ($value:expr) => ($value)
+}
+
+#[macro_export]
+macro_rules! assert_matches {
+ ($expression:expr, $($pattern:tt)+) => {
+ _matches_tt_as_expr_hack! {
+ match $expression {
+ $($pattern)+ => (),
+ ref e => panic!("assertion failed: `{:?}` does not match `{}`", e, stringify!($($pattern)+)),
+ }
+ }
+ }
+}
+
+#[macro_export]
+macro_rules! debug_assert_matches {
+ ($($arg:tt)*) => (if cfg!(debug_assertions) { assert_matches!($($arg)*); })
+}
+
+#[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')
+ );
+}