Macro konst::coerce_to_cmp

source ·
macro_rules! coerce_to_cmp {
    ($reference:expr $(,)*) => { ... };
    ($left:expr, $right:expr $(,)*) => { ... };
}
Expand description

Coerces reference to a type that has a const_eq or const_cmp method.

§Behavior

This requires arguments to implement the ConstCmp trait.

When a type from the standard library is passed, this wraps it inside a CmpWrapper, which declares const_eq and const_cmp methods for many standard library types.

When a user-defined type is used, this evaluates to a reference to the passed in value, dereferencing it as necessary.

§Limitations

The parameter(s) 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::{
    cmp::CmpWrapper,
    coerce_to_cmp, impl_cmp,
};

struct Unit;

impl_cmp!{
    impl Unit;
     
    pub const fn const_eq(&self, other: &Self) -> bool {
        true
    }
}

let wrapper: CmpWrapper<i32> = coerce_to_cmp!(0i32);
assert!( wrapper.const_eq(&0));
assert!(!wrapper.const_eq(&1));

let unit: &Unit = coerce_to_cmp!(Unit);
assert!( unit.const_eq(&Unit));