1
Fork 0

Apply suggestion

This commit is contained in:
Benoît du Garreau 2020-12-09 11:29:42 +01:00
parent 52197d356c
commit f138e260a0
5 changed files with 33 additions and 40 deletions

View file

@ -4,28 +4,7 @@ use crate::{fmt, panic};
#[doc(hidden)]
#[unstable(feature = "macros_internals", reason = "macros implementation detail", issue = "none")]
#[track_caller]
pub fn assert_failed<T, U>(op: &str, left: &T, right: &U) -> !
where
T: fmt::Debug + ?Sized,
U: fmt::Debug + ?Sized,
{
#[track_caller]
fn inner(op: &str, left: &dyn fmt::Debug, right: &dyn fmt::Debug) -> ! {
panic!(
r#"assertion failed: `(left {} right)`
left: `{:?}`,
right: `{:?}`"#,
op, left, right
)
}
inner(op, &left, &right)
}
#[cold]
#[doc(hidden)]
#[unstable(feature = "macros_internals", reason = "macros implementation detail", issue = "none")]
#[track_caller]
pub fn assert_failed_args<T, U>(op: &str, left: &T, right: &U, args: fmt::Arguments<'_>) -> !
pub fn assert_failed<T, U>(op: &str, left: &T, right: &U, args: Option<fmt::Arguments<'_>>) -> !
where
T: fmt::Debug + ?Sized,
U: fmt::Debug + ?Sized,
@ -35,14 +14,22 @@ where
op: &str,
left: &dyn fmt::Debug,
right: &dyn fmt::Debug,
args: fmt::Arguments<'_>,
args: Option<fmt::Arguments<'_>>,
) -> ! {
panic!(
r#"assertion failed: `(left {} right)`
match args {
Some(args) => panic!(
r#"assertion failed: `(left {} right)`
left: `{:?}`,
right: `{:?}: {}`"#,
op, left, right, args
)
op, left, right, args
),
None => panic!(
r#"assertion failed: `(left {} right)`
left: `{:?}`,
right: `{:?}`"#,
op, left, right,
),
}
}
inner(op, &left, &right, args)
}

View file

@ -66,7 +66,7 @@ macro_rules! assert_eq {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
$crate::macros_internals::assert_failed("==", &*left_val, &*right_val);
$crate::macros_internals::assert_failed("==", &*left_val, &*right_val, $crate::option::Option::None);
}
}
}
@ -78,7 +78,7 @@ macro_rules! assert_eq {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
$crate::macros_internals::assert_failed_args("==", &*left_val, &*right_val, $crate::format_args!($($arg)+));
$crate::macros_internals::assert_failed("==", &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
}
}
}
@ -113,7 +113,7 @@ macro_rules! assert_ne {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
$crate::macros_internals::assert_failed("!=", &*left_val, &*right_val);
$crate::macros_internals::assert_failed("!=", &*left_val, &*right_val, $crate::option::Option::None);
}
}
}
@ -125,7 +125,7 @@ macro_rules! assert_ne {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
$crate::macros_internals::assert_failed_args("!=", &*left_val, &*right_val, $crate::format_args!($($arg)+));
$crate::macros_internals::assert_failed("!=", &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
}
}
}