Trait FmtOrd

Source
pub trait FmtOrd:
    Display
    + Ord
    + FmtEq { }
Expand description

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

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

assert_eq!(a.cmp(&b), (*format!("{}", a)).cmp(&format!("{}", b)));

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

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

§Colloraries

From str: Ord and the above property, it follows that T satisfies Ord trait’s contract.

§Examples

Integer primitives do not satisfy the property.

assert!(42 < 240);
// but...
assert!(42.to_string() > 240.to_string());

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

assert!(fmt_cmp::Cmp(42) > fmt_cmp::Cmp(240));

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 FmtOrd for Infallible

Source§

impl FmtOrd for bool

Source§

impl FmtOrd for str

Source§

impl FmtOrd for String

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Implementors§

Source§

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