Trait konst::polymorphism::ConstCmpMarker[][src]

pub trait ConstCmpMarker {
    type Kind;
    type This: ?Sized;
}
This is supported on crate feature cmp only.

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 and This associated types are used in the IsAConstCmpMarker marker type to automatically wrap types in CmpWrapper if they’re from the standard library, otherwise leaving them unwrapped.

Example

Manual Implementation

use konst::{
    polymorphism::{ConstCmpMarker, IsNotStdKind},
    const_cmp, const_eq, try_equal,
};

use std::cmp::Ordering;


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

impl ConstCmpMarker for MyType {
    // These are the only associated types you're expected to use in
    // impls for custom types.
    type Kind = IsNotStdKind;
    type This = Self;
}

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 CMPS: [(Ordering, bool); 4] = {
    let foo = MyType{x: "hello", y: &[3, 5, 8, 13]};
    let bar = MyType{x: "world", y: &[3, 5, 8, 13]};

    [
        (const_cmp!(foo, foo), const_eq!(foo, foo)),
        (const_cmp!(foo, bar), const_eq!(foo, bar)),
        (const_cmp!(bar, foo), const_eq!(bar, foo)),
        (const_cmp!(bar, bar), const_eq!(bar, bar)),
    ]
};

assert_eq!(
    CMPS,
    [
        (Ordering::Equal, true),
        (Ordering::Less, false),
        (Ordering::Greater, false),
        (Ordering::Equal, true),
    ]
);


ì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 CMPS: [(Ordering, bool); 4] = {
    let foo = MyType{x: "hello", y: &[3u16, 5, 8, 13]};
    let bar = MyType{x: "world", y: &[3, 5, 8, 13]};

    [
        (const_cmp!(foo, foo), const_eq!(foo, foo)),
        (const_cmp!(foo, bar), const_eq!(foo, bar)),
        (const_cmp!(bar, foo), const_eq!(bar, foo)),
        (const_cmp!(bar, bar), const_eq!(bar, bar)),
    ]
};

assert_eq!(
    CMPS,
    [
        (Ordering::Equal, true),
        (Ordering::Less, false),
        (Ordering::Greater, false),
        (Ordering::Equal, true),
    ]
);

Associated Types

type Kind[src]

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

type This: ?Sized[src]

The type after dereferencing, implemented as type This = Self; for all non-reference types

Loading content...

Implementations on Foreign Types

impl ConstCmpMarker for Option<u8>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<i8>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<u16>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<i16>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<u32>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<i32>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<u64>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<i64>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<u128>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<i128>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<usize>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<isize>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<bool>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<char>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Range<u8>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Range<u16>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Range<u32>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Range<u64>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Range<u128>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Range<usize>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Range<char>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for RangeInclusive<u8>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for RangeInclusive<u16>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for RangeInclusive<u32>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for RangeInclusive<u64>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for RangeInclusive<u128>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for RangeInclusive<usize>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for RangeInclusive<char>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroU8[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroI8[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroU16[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroI16[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroU32[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroI32[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroU64[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroI64[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroU128[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroI128[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroUsize[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for NonZeroIsize[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroU8>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroI8>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroU16>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroI16>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroU32>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroI32>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroU64>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroI64>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroU128>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroI128>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroUsize>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<NonZeroIsize>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Ordering[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for Option<Ordering>[src]

type Kind = IsStdKind

type This = Self

impl<T> ConstCmpMarker for PhantomData<T>[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for PhantomPinned[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [u8]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [u16]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [u32]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [u64]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [u128]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [i8]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [i16]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [i32]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [i64]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [i128]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [bool]>[src]

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a [char]>[src]

type Kind = IsStdKind

type This = Self

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

type Kind = IsStdKind

type This = Self

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

type Kind = IsStdKind

type This = Self

impl<'a> ConstCmpMarker for Option<&'a str>[src]

type Kind = IsStdKind

This is supported on crate feature cmp only.

type This = Self

This is supported on crate feature cmp only.
Loading content...

Implementors

impl ConstCmpMarker for bool[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for char[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for i8[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for i16[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for i32[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for i64[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for i128[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for isize[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for str[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for u8[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for u16[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for u32[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for u64[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for u128[src]

type Kind = IsStdKind

type This = Self

impl ConstCmpMarker for usize[src]

type Kind = IsStdKind

type This = Self

impl<P> ConstCmpMarker for CmpWrapper<P>[src]

type Kind = IsNotStdKind

type This = Self

impl<T> ConstCmpMarker for [T; 0][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 1][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 2][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 3][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 4][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 5][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 6][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 7][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 8][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 9][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 10][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 11][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 12][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 13][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 14][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 15][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 16][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 17][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 18][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 19][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 20][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 21][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 22][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 23][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 24][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 25][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 26][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 27][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 28][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 29][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 30][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 31][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T; 32][src]

type Kind = IsArrayKind<T>

type This = Self

impl<T> ConstCmpMarker for [T][src]

type Kind = IsArrayKind<T>

type This = [T]

impl<T: ?Sized> ConstCmpMarker for &T where
    T: ConstCmpMarker
[src]

type Kind = T::Kind

type This = T::This

impl<T: ?Sized> ConstCmpMarker for &mut T where
    T: ConstCmpMarker
[src]

type Kind = T::Kind

type This = T::This

Loading content...