Implementing Cmp for custom Enum

I have the following enum:

enum Foo {
    Foo(u8),
    Bar(u8),
    Baz(u8)
}

I'd like to implement ParitalOrd such that Foo(x) == Bar(x) == Baz(x), but for x > y > z, Foo(x) > Bar(y) > Baz(z). What is the appropriate syntax for implementing that?

This seems to work for the question I've asked but my program is not working correctly, so I guess I have an X/Y problem to figure out.

Uggh. Was sorting on wrong field of struct. The playground link works for my purposes but advice on improvements to the methods are welcome.

If

impl Ord for Card {
    fn cmp(&self, other: &Self) -> Ordering {
        self.unwrap().cmp(&other.unwrap())
    }
}

then

impl PartialEq for Card {
    fn eq(&self, other: &Self) -> bool {
        self.unwrap() == other.unwrap()
    }
}
1 Like

I'd recommend not calling your method unwrap, that sort of implies the function could panic in some cases, which is not the case for you.

I suppose if I were exposing this publicly I would change it to extract() or something. Good to keep in mind though for future projects. Thanks.