@@ -97,6 +97,7 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
9797pub enum AssertKind {
9898 Eq ,
9999 Ne ,
100+ Match ,
100101}
101102
102103/// Internal function for `assert_eq!` and `assert_ne!` macros
@@ -113,32 +114,54 @@ where
113114 T : fmt:: Debug + ?Sized ,
114115 U : fmt:: Debug + ?Sized ,
115116{
116- #[ track_caller]
117- fn inner (
118- kind : AssertKind ,
119- left : & dyn fmt:: Debug ,
120- right : & dyn fmt:: Debug ,
121- args : Option < fmt:: Arguments < ' _ > > ,
122- ) -> ! {
123- let op = match kind {
124- AssertKind :: Eq => "==" ,
125- AssertKind :: Ne => "!=" ,
126- } ;
127-
128- match args {
129- Some ( args) => panic ! (
130- r#"assertion failed: `(left {} right)`
117+ assert_failed_inner ( kind, & left, & right, args)
118+ }
119+
120+ /// Internal function for `assert_match!`
121+ #[ cold]
122+ #[ track_caller]
123+ #[ doc( hidden) ]
124+ pub fn assert_matches_failed < T : fmt:: Debug + ?Sized > (
125+ left : & T ,
126+ right : & str ,
127+ args : Option < fmt:: Arguments < ' _ > > ,
128+ ) -> ! {
129+ // Use the Display implementation to display the pattern.
130+ struct Pattern < ' a > ( & ' a str ) ;
131+ impl fmt:: Debug for Pattern < ' _ > {
132+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
133+ fmt:: Display :: fmt ( self . 0 , f)
134+ }
135+ }
136+ assert_failed_inner ( AssertKind :: Match , & left, & Pattern ( right) , args) ;
137+ }
138+
139+ /// Non-generic version of the above functions, to avoid code bloat.
140+ #[ track_caller]
141+ fn assert_failed_inner (
142+ kind : AssertKind ,
143+ left : & dyn fmt:: Debug ,
144+ right : & dyn fmt:: Debug ,
145+ args : Option < fmt:: Arguments < ' _ > > ,
146+ ) -> ! {
147+ let op = match kind {
148+ AssertKind :: Eq => "==" ,
149+ AssertKind :: Ne => "!=" ,
150+ AssertKind :: Match => "matches" ,
151+ } ;
152+
153+ match args {
154+ Some ( args) => panic ! (
155+ r#"assertion failed: `(left {} right)`
131156 left: `{:?}`,
132157 right: `{:?}: {}`"# ,
133- op, left, right, args
134- ) ,
135- None => panic ! (
136- r#"assertion failed: `(left {} right)`
158+ op, left, right, args
159+ ) ,
160+ None => panic ! (
161+ r#"assertion failed: `(left {} right)`
137162 left: `{:?}`,
138163 right: `{:?}`"# ,
139- op, left, right,
140- ) ,
141- }
164+ op, left, right,
165+ ) ,
142166 }
143- inner ( kind, & left, & right, args)
144167}
0 commit comments