FmtEq

Trait FmtEq 

Source
pub trait FmtEq: Display + Eq { }
Expand description

A marker trait for types whose equivalence relation is the same as equivalence between its Display representation.

When T implements FmtEq, the following property must be upheld for all a: T and b: T:

assert_eq!(a == b, *format!("{}", a) == *format!("{}", b));

In other words (assuming that no ill-defined specialization is involved):

a == b <-> a.to_string() == b.to_string()

§Corollaries

From str: Eq and the above property, it follows that T satisfies Eq trait’s contract, i.e., reflexivity, symmetricity and transitivity of == operator.

§Examples

Floating-point number primitives do not satisfy the property (they are not even Eq):

assert_eq!(0.0, -0.0);
assert_ne!(0.0.to_string(), (-0.0).to_string());

assert_ne!(f64::NAN, f64::NAN);
assert_eq!(f64::NAN.to_string(), f64::NAN.to_string());

Wrapping any Display type with fmt_cmp::Cmp makes it FmtEq:

assert_ne!(fmt_cmp::Cmp(0.0), fmt_cmp::Cmp(-0.0));
assert_eq!(fmt_cmp::Cmp(f64::NAN), fmt_cmp::Cmp(f64::NAN));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FmtEq for Infallible

Source§

impl FmtEq for bool

Source§

impl FmtEq for i8

Source§

impl FmtEq for i16

Source§

impl FmtEq for i32

Source§

impl FmtEq for i64

Source§

impl FmtEq for i128

Source§

impl FmtEq for isize

Source§

impl FmtEq for str

Source§

impl FmtEq for u8

Source§

impl FmtEq for u16

Source§

impl FmtEq for u32

Source§

impl FmtEq for u64

Source§

impl FmtEq for u128

Source§

impl FmtEq for usize

Source§

impl FmtEq for String

Available on crate feature alloc only.
Source§

impl<P: Borrow<<P as Deref>::Target> + Deref + Display> FmtEq for Pin<P>
where P::Target: FmtEq,

Source§

impl<T: FmtEq + ToOwned + ?Sized> FmtEq for Cow<'_, T>
where T::Owned: Display,

Available on crate feature alloc only.
Source§

impl<T: FmtEq + ?Sized> FmtEq for &T

Source§

impl<T: FmtEq + ?Sized> FmtEq for &mut T

Source§

impl<T: FmtEq + ?Sized> FmtEq for Box<T>

Available on crate feature alloc only.
Source§

impl<T: FmtEq + ?Sized> FmtEq for Rc<T>

Available on crate feature alloc only.
Source§

impl<T: FmtEq + ?Sized> FmtEq for Arc<T>

Available on crate feature alloc only.

Implementors§

Source§

impl<T: Display + ?Sized> FmtEq for Cmp<T>