Trait konst::cmp::ConstCmp

source ·
pub trait ConstCmp {
    type Kind;
}
Available on crate feature cmp only.
Expand description

Marker trait for types that implement the const comparison methods.

§Implementors

Types that implement this trait are also expected to implement at least one of these inherent methods:

use std::cmp::Ordering;

const fn const_eq(&self, other: &Self) -> bool

const fn const_cmp(&self, other: &Self) -> Ordering

§Coercions

The Kind associated type is used in the IsAConstCmp marker type to automatically wrap types in CmpWrapper if they’re from the standard library, otherwise leaving them unwrapped.

§Example

§Manual Implementation

use konst::{
    cmp::{ConstCmp, IsNotStdKind},
    const_cmp, const_eq, try_equal,
};

use std::cmp::Ordering;


struct MyType {
    x: &'static str,
    y: &'static [u16],
}

impl ConstCmp for MyType {
    type Kind = IsNotStdKind;
}

impl MyType {
    pub const fn const_eq(&self, other: &Self) -> bool {
        const_eq!(self.x, other.x) &&
        const_eq!(self.y, other.y)
    }

    pub const fn const_cmp(&self, other: &Self) -> Ordering {
        try_equal!(const_cmp!(self.x, other.x));
        try_equal!(const_cmp!(self.y, other.y))
    }
}

const _: () = {
    let foo = MyType{x: "hello", y: &[3, 5, 8, 13]};
    let bar = MyType{x: "world", y: &[3, 5, 8, 13]};

    assert!(matches!(const_cmp!(foo, foo), Ordering::Equal));
    assert!(matches!(const_cmp!(foo, bar), Ordering::Less));
    assert!(matches!(const_cmp!(bar, foo), Ordering::Greater));
    assert!(const_eq!(foo, foo));
    assert!(!const_eq!(foo, bar));
};

§ìmpl_cmp-based Implementation

You can use impl_cmp to implement this trait, as well as define the same methods for multiple implementations with different type arguments.

use konst::{const_cmp, const_eq, impl_cmp, try_equal};

use std::cmp::Ordering;


struct MyType<'a, T> {
    x: &'a str,
    y: &'a [T],
}

impl_cmp!{
    // The comparison functions are only implemented for these types.
    impl['a] MyType<'a, bool>;
    impl['a] MyType<'a, u16>;
    impl['a] MyType<'a, &'static str>;

    pub const fn const_eq(&self, other: &Self) -> bool {
        const_eq!(self.x, other.x) &&
        const_eq!(self.y, other.y)
    }

    pub const fn const_cmp(&self, other: &Self) -> Ordering {
        try_equal!(const_cmp!(self.x, other.x));
        try_equal!(const_cmp!(self.y, other.y))
    }
}

const _: () = {
    let foo = MyType{x: "hello", y: &[3, 5, 8, 13]};
    let bar = MyType{x: "world", y: &[3, 5, 8, 13]};

    assert!(matches!(const_cmp!(foo, foo), Ordering::Equal));
    assert!(matches!(const_cmp!(foo, bar), Ordering::Less));
    assert!(matches!(const_cmp!(bar, foo), Ordering::Greater));
    assert!(const_eq!(foo, foo));
    assert!(!const_eq!(foo, bar));
};

const _: () = {
    let foo = MyType{x: "hello", y: &[false]};
    let bar = MyType{x: "hello", y: &[true]};

    assert!(matches!(const_cmp!(foo, foo), Ordering::Equal));
    assert!(matches!(const_cmp!(foo, bar), Ordering::Less));
    assert!(matches!(const_cmp!(bar, foo), Ordering::Greater));
    assert!(const_eq!(foo, foo));
    assert!(!const_eq!(foo, bar));
};

Required Associated Types§

source

type Kind

What kind of type this is, this can be one of:

Implementations on Foreign Types§

source§

impl ConstCmp for Ordering

source§

impl ConstCmp for Option<Ordering>

source§

impl ConstCmp for Option<bool>

source§

impl ConstCmp for Option<char>

source§

impl ConstCmp for Option<i8>

source§

impl ConstCmp for Option<i16>

source§

impl ConstCmp for Option<i32>

source§

impl ConstCmp for Option<i64>

source§

impl ConstCmp for Option<i128>

source§

impl ConstCmp for Option<isize>

source§

impl ConstCmp for Option<u8>

source§

impl ConstCmp for Option<u16>

source§

impl ConstCmp for Option<u32>

source§

impl ConstCmp for Option<u64>

source§

impl ConstCmp for Option<u128>

source§

impl ConstCmp for Option<usize>

source§

impl ConstCmp for Option<NonZeroI8>

source§

impl ConstCmp for Option<NonZeroI16>

source§

impl ConstCmp for Option<NonZeroI32>

source§

impl ConstCmp for Option<NonZeroI64>

source§

impl ConstCmp for Option<NonZeroI128>

source§

impl ConstCmp for Option<NonZeroIsize>

source§

impl ConstCmp for Option<NonZeroU8>

source§

impl ConstCmp for Option<NonZeroU16>

source§

impl ConstCmp for Option<NonZeroU32>

source§

impl ConstCmp for Option<NonZeroU64>

source§

impl ConstCmp for Option<NonZeroU128>

source§

impl ConstCmp for Option<NonZeroUsize>

source§

impl ConstCmp for bool

source§

impl ConstCmp for char

source§

impl ConstCmp for i8

source§

impl ConstCmp for i16

source§

impl ConstCmp for i32

source§

impl ConstCmp for i64

source§

impl ConstCmp for i128

source§

impl ConstCmp for isize

source§

impl ConstCmp for str

source§

impl ConstCmp for u8

source§

impl ConstCmp for u16

source§

impl ConstCmp for u32

source§

impl ConstCmp for u64

source§

impl ConstCmp for u128

source§

impl ConstCmp for usize

source§

impl ConstCmp for PhantomPinned

source§

impl ConstCmp for Range<char>

source§

impl ConstCmp for Range<u8>

source§

impl ConstCmp for Range<u16>

source§

impl ConstCmp for Range<u32>

source§

impl ConstCmp for Range<u64>

source§

impl ConstCmp for Range<u128>

source§

impl ConstCmp for Range<usize>

source§

impl ConstCmp for RangeInclusive<char>

source§

impl ConstCmp for RangeInclusive<u8>

source§

impl ConstCmp for RangeInclusive<u16>

source§

impl ConstCmp for RangeInclusive<u32>

source§

impl ConstCmp for RangeInclusive<u64>

source§

impl ConstCmp for RangeInclusive<u128>

source§

impl ConstCmp for RangeInclusive<usize>

source§

impl ConstCmp for NonZeroI8

source§

impl ConstCmp for NonZeroI16

source§

impl ConstCmp for NonZeroI32

source§

impl ConstCmp for NonZeroI64

source§

impl ConstCmp for NonZeroI128

source§

impl ConstCmp for NonZeroIsize

source§

impl ConstCmp for NonZeroU8

source§

impl ConstCmp for NonZeroU16

source§

impl ConstCmp for NonZeroU32

source§

impl ConstCmp for NonZeroU64

source§

impl ConstCmp for NonZeroU128

source§

impl ConstCmp for NonZeroUsize

source§

impl<'a> ConstCmp for Option<&'a str>

§

type Kind = IsStdKind

Available on crate feature cmp only.
source§

impl<'a> ConstCmp for Option<&'a [bool]>

source§

impl<'a> ConstCmp for Option<&'a [char]>

source§

impl<'a> ConstCmp for Option<&'a [i8]>

source§

impl<'a> ConstCmp for Option<&'a [i16]>

source§

impl<'a> ConstCmp for Option<&'a [i32]>

source§

impl<'a> ConstCmp for Option<&'a [i64]>

source§

impl<'a> ConstCmp for Option<&'a [i128]>

source§

impl<'a> ConstCmp for Option<&'a [isize]>

source§

impl<'a> ConstCmp for Option<&'a [u8]>

§

type Kind = IsStdKind

Available on crate feature cmp only.
source§

impl<'a> ConstCmp for Option<&'a [u16]>

source§

impl<'a> ConstCmp for Option<&'a [u32]>

source§

impl<'a> ConstCmp for Option<&'a [u64]>

source§

impl<'a> ConstCmp for Option<&'a [u128]>

source§

impl<'a> ConstCmp for Option<&'a [usize]>

source§

impl<'a, 'b> ConstCmp for Option<&'a [&'b str]>

source§

impl<'a, 'b> ConstCmp for Option<&'a [&'b [u8]]>

source§

impl<T> ConstCmp for &T
where T: ?Sized + ConstCmp,

source§

impl<T> ConstCmp for &mut T
where T: ?Sized + ConstCmp,

source§

impl<T> ConstCmp for [T]

source§

impl<T> ConstCmp for PhantomData<T>

source§

impl<T, const N: usize> ConstCmp for [T; N]

Implementors§