Macro konst::const_eq

source ·
macro_rules! const_eq {
    ($left:expr, $right:expr $(,)*) => { ... };
}
Available on crate feature cmp only.
Expand description

Compares two values for equality.

The arguments must implement the ConstCmp trait. Non-standard library types must define a const_eq method taking a reference.

§Limitations

The arguments must be concrete types, and have a fully inferred type. eg: if you pass an integer literal it must have a suffix to indicate its type.

§Example

use konst::{const_eq, impl_cmp};

use std::ops::Range;

struct Fields<'a> {
    foo: u32,
    bar: Option<bool>,
    baz: Range<usize>,
    qux: &'a str,
}

impl_cmp!{
    impl['a] Fields<'a>;
    pub const fn const_eq(&self, other: &Self) -> bool {
        self.foo == other.foo &&
        const_eq!(self.bar, other.bar) &&
        const_eq!(self.baz, other.baz) &&
        const_eq!(self.qux, other.qux)
    }
}

const _: () = {
    let foo = Fields {
        foo: 10,
        bar: None,
        baz: 10..20,
        qux: "hello",
    };
     
    let bar = Fields {
        foo: 99,
        bar: Some(true),
        baz: 0..5,
        qux: "world",
    };
     
    assert!( const_eq!(foo, foo));
    assert!(!const_eq!(foo, bar));
    assert!(!const_eq!(bar, foo));
    assert!( const_eq!(bar, bar));
};