@@ -110,7 +110,10 @@ macro_rules! assert_ne {
110
110
} ) ;
111
111
}
112
112
113
- /// Asserts that an expression matches a pattern.
113
+ /// Asserts that an expression matches any of the given patterns.
114
+ ///
115
+ /// Like in a `match` expression, the pattern can be optionally followed by `if`
116
+ /// and a guard expression that has access to names bound by the pattern.
114
117
///
115
118
/// On panic, this macro will print the value of the expression with its
116
119
/// debug representation.
@@ -125,38 +128,35 @@ macro_rules! assert_ne {
125
128
/// let b = 1u32.checked_sub(2);
126
129
/// assert_matches!(a, Some(_));
127
130
/// assert_matches!(b, None);
131
+ ///
132
+ /// let c = Ok("abc".to_string());
133
+ /// assert_matches!(a, Ok(x) | Err(x) if x.len() < 100);
128
134
/// ```
129
135
#[ macro_export]
130
136
#[ unstable( feature = "assert_matches" , issue = "none" ) ]
131
137
#[ allow_internal_unstable( core_panic) ]
132
138
macro_rules! assert_matches {
133
- ( $left: expr, $right: pat $( , ) ?) => ( {
134
- match & $left {
139
+ ( $left: expr, $( $pattern: pat ) |+ $( if $guard: expr ) ? $( , ) ?) => ( {
140
+ match $left {
141
+ $( $pattern ) |+ $( if $guard ) ? => { }
135
142
left_val => {
136
- if let $right = left_val {
137
- // OK
138
- } else {
139
- $crate:: panicking:: assert_matches_failed(
140
- & * left_val,
141
- $crate:: stringify!( $right) ,
142
- $crate:: option:: Option :: None
143
- ) ;
144
- }
143
+ $crate:: panicking:: assert_matches_failed(
144
+ & left_val,
145
+ $crate:: stringify!( $( $pattern) |+ $( if $guard) ?) ,
146
+ $crate:: option:: Option :: None
147
+ ) ;
145
148
}
146
149
}
147
150
} ) ;
148
- ( $left: expr, $right: expr, $( $arg: tt) +) => ( {
149
- match & $left {
151
+ ( $left: expr, $( $pattern: pat ) |+ $( if $guard: expr ) ?, $( $arg: tt) +) => ( {
152
+ match $left {
153
+ $( $pattern ) |+ $( if $guard ) ? => { }
150
154
left_val => {
151
- if let $right = left_val {
152
- // OK
153
- } else {
154
- $crate:: panicking:: assert_matches_failed(
155
- & * left_val,
156
- $crate:: stringify!( $right) ,
157
- $crate:: option:: Option :: Some ( $crate:: format_args!( $( $arg) +) )
158
- ) ;
159
- }
155
+ $crate:: panicking:: assert_matches_failed(
156
+ & left_val,
157
+ $crate:: stringify!( $( $pattern) |+ $( if $guard) ?) ,
158
+ $crate:: option:: Option :: Some ( $crate:: format_args!( $( $arg) +) )
159
+ ) ;
160
160
}
161
161
}
162
162
} ) ;
0 commit comments