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

pub trait ConstCmpMarker {
    type Kind;
    type This: ?Sized;
}
This is supported 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 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

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

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

Implementations on Foreign Types

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

Implementors