matches-0.1.10/.cargo_vcs_info.json0000644000000001450000000000100125370ustar { "git": { "sha1": "e66ab5acd06085513490fd8ea41a61ad556c0cf9" }, "path_in_vcs": "matches" }matches-0.1.10/Cargo.toml0000644000000014260000000000100105400ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] name = "matches" version = "0.1.10" description = "A macro to evaluate, as a boolean, whether an expression matches a pattern." documentation = "https://docs.rs/matches/" readme = "README.md" license = "MIT" repository = "https://github.com/SimonSapin/rust-std-candidates" [lib] name = "matches" path = "lib.rs" matches-0.1.10/Cargo.toml.orig000064400000000000000000000004561046102023000142230ustar 00000000000000[package] name = "matches" version = "0.1.10" 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" matches-0.1.10/LICENSE000064400000000000000000000020441046102023000123340ustar 00000000000000Copyright (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. matches-0.1.10/README.md000064400000000000000000000005661046102023000126150ustar 00000000000000A macro to evaluate, as a boolean, whether an expression matches a pattern. For users who build using only Rust 1.42 and newer, consider using [`std::matches`], which is included in the [standard library prelude] and thus is automatically in scope. [`std::matches`]: core::matches [standard library prelude]: https://doc.rust-lang.org/stable/reference/names/preludes.html matches-0.1.10/lib.rs000064400000000000000000000074551046102023000124560ustar 00000000000000#![no_std] //! A macro to evaluate, as a boolean, whether an expression matches a pattern. //! //! For users who build using only Rust 1.42 and newer, consider using [`std::matches`], which //! is included in the [standard library prelude] and thus is automatically in scope. //! //! [`std::matches`]: core::matches //! [standard library prelude]: https://doc.rust-lang.org/stable/reference/names/preludes.html //! //! # Examples //! //! ``` //! #[macro_use] //! extern crate matches; //! //! #[derive(Debug)] //! pub enum Foo { //! A, //! B(T), //! } //! //! impl Foo { //! pub fn is_b(&self) -> bool { //! matches!(*self, Foo::B(_)) //! } //! } //! //! impl Foo { //! pub fn assert_is_b(&self) { //! assert_matches!(&self, Foo::B(_)); //! } //! } //! # fn main() { } //! ``` /// 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 { /// A, /// B(T), /// } /// /// impl Foo { /// 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') ); } matches-0.1.10/tests/macro_use_one.rs000064400000000000000000000004721046102023000156600ustar 00000000000000// 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') )); } matches-0.1.10/tests/use_star.rs000064400000000000000000000002501046102023000146610ustar 00000000000000//! https://github.com/SimonSapin/rust-std-candidates/issues/22 extern crate matches; use matches::*; #[test] fn test_assert_matches() { assert_matches!(4, 4) }