Closed
Description
In order to use assert_eq!
in constants, we first need to have panic!
with formatting (#51999) and const fn
PartialEq::eq
implementations (#67792) implemented and stabilized. If you look at the desugaring of assert_eq!
, this quickly becomes obvious:
assert_eq!(a, b);
desugars to
match (&a, &b) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
// 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.
panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`"#, &*left_val, &*right_val)
}
}
}
where the ==
operation will desugar to PartialEq::eq(left_val, right_val)
for all non-primites. If we want to, we can support assert_eq!
for primitives (integers, chars, bools) with only panicking with formatting.