Macro konst::const_eq[][src]

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

Compares two values for equality.

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


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.


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['a] Fields<'a>;
    pub const fn const_eq(&self, other: &Self) -> bool { == &&
        const_eq!(, &&
        const_eq!(self.baz, other.baz) &&
        const_eq!(self.qux, other.qux)

const CMPS: [bool; 4] = {
    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",
    [const_eq!(foo, foo), const_eq!(foo, bar), const_eq!(bar, foo), const_eq!(bar, bar)]

assert_eq!(CMPS, [true, false, false, true]);