Compare enums only by variant, not value

As of Rust 1.21.0, you can use std::mem::discriminant:

fn variant_eq(a: &Op, b: &Op) -> bool {
    std::mem::discriminant(a) == std::mem::discriminant(b)
}

This is nice because it can be very generic:

fn variant_eq<T>(a: &T, b: &T) -> bool {
    std::mem::discriminant(a) == std::mem::discriminant(b)
}

Before Rust 1.21.0, I’d match on the tuple of both arguments and ignore the contents of the tuple with _ or ..:

struct Add(u8);
struct Sub(u8);

enum Op {
    Add(Add),
    Sub(Sub),
}

fn variant_eq(a: &Op, b: &Op) -> bool {
    match (a, b) {
        (&Op::Add(..), &Op::Add(..)) => true,
        (&Op::Sub(..), &Op::Sub(..)) => true,
        _ => false,
    }
}

fn main() {
    let a = Op::Add(Add(42));
    
    let b = Op::Add(Add(42));
    let c = Op::Add(Add(21));
    let d = Op::Sub(Sub(42));

    println!("{}", variant_eq(&a, &b));
    println!("{}", variant_eq(&a, &c));
    println!("{}", variant_eq(&a, &d));
}

I took the liberty of renaming the function though, as the components of enums are called variants, and really you are testing to see if they are equal, not comparing them (which is usually used for ordering / sorting).

For performance, let’s look at the LLVM IR in generated by Rust 1.60.0 in release mode (and marking variant_eq as #[inline(never)]). The Rust Playground can show you this:

; playground::variant_eq
; Function Attrs: mustprogress nofree noinline norecurse nosync nounwind nonlazybind readonly uwtable willreturn
define internal fastcc noundef zeroext i1 @_ZN10playground10variant_eq17hc64d59c7864eb861E(i8 %a.0.0.val, i8 %b.0.0.val) unnamed_addr #2 {
start:
  %_8.not = icmp eq i8 %a.0.0.val, %b.0.0.val
  ret i1 %_8.not
}

This code directly compares the variant discriminant.

If you wanted to have a macro to generate the function, something like this might be good start.

struct Add(u8);
struct Sub(u8);

macro_rules! foo {
        (enum $name:ident {
            $($vname:ident($inner:ty),)*
        }) => {
            enum $name {
                 $($vname($inner),)*
            }

            impl $name {
                fn variant_eq(&self, b: &Self) -> bool {
                    match (self, b) {
                        $((&$name::$vname(..), &$name::$vname(..)) => true,)*
                        _ => false,
                    }
                }
            }
        }
    }

foo! {
    enum Op {
        Add(Add),
        Sub(Sub),
    }
}

fn main() {
    let a = Op::Add(Add(42));

    let b = Op::Add(Add(42));
    let c = Op::Add(Add(21));
    let d = Op::Sub(Sub(42));

    println!("{}", Op::variant_eq(&a, &b));
    println!("{}", Op::variant_eq(&a, &c));
    println!("{}", Op::variant_eq(&a, &d));
}

The macro does have limitations though – all the variants need to have a single variant. Supporting unit variants, variants with more than one type, struct variants, visibility, etc are all real hard. Perhaps a procedural macro would make it a bit easier.

Leave a Comment