1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

/// Evaluates to true if the expression matches any of the patterns
/// (this macro can have multiple patterns).
///
/// This is equivalent to the [`std::matches`] macro, which requires Rust 1.42.0 .
///
/// # Example
///
/// ```
/// use core_extensions::matches;
///
/// let some = Some(10);
/// assert!( matches!(some, Some(10)));
/// assert!( matches!(some, Some(x) if x == 10));
/// assert!(!matches!(some, None));
///
/// let none = None;
/// assert!(!matches!(none, Some(10)));
/// assert!(!matches!(none, Some(x) if x == 10));
/// assert!( matches!(none, None));
///
/// 
/// for num in &[0, 1, 2, 3][..] {
///     assert!(matches!(num, 0 | 1 | 2 | 3))
/// }
/// 
/// 
/// enum Primitive {
///     Signed(i128),
///     Unsigned(u128),
///     Bool(bool),
///     String(&'static str),
/// }
/// 
/// let prim = Primitive::Bool(false);
/// assert!(matches!(
///     prim,
///     | Primitive::Signed(_)
///     | Primitive::Unsigned(_)
///     | Primitive::Bool(_)
/// ));
/// 
/// ```
/// 
/// [`std::matches`]: https://doc.rust-lang.org/std/macro.matches.html
#[macro_export]
macro_rules! matches {
    ( $expr:expr, $(|)? $pat:pat $(| $prev_pat:pat)* $(if $cond:expr)?)=>{
        match $expr {
            $pat $( | $prev_pat)* =>true,
            _=>false
        }
    };
}