0

I'm trying to define equality for a specific trait's implementors. This is a very simplified version of the problem I'm trying to solve:

trait Tree {}

struct Plum;
struct Cherry;

impl Tree for Plum {}
impl Tree for Cherry {}

impl PartialEq for Tree {
    fn eq(&self, other: &Tree) -> bool {
        match (self, other) {
            (Plum, Plum) => true,
            (Cherry, Cherry) => true,
            _ => false,
        }
    }
}
fn main() {
    let p: &Tree = &Plum {};
    let c: &Tree = &Cherry {};
    println!("{}", p == c);
}

I realize that in this simplified version I can just derive equality, but my actual problem is more involved. For example, there might be something like:

struct Graftage<'a> {
    scion: &'a mut Tree,
    stock: &'a mut Tree,
}

impl Tree for Graftage<'a> {}

I also tried this variation:

#[derive(PartialEq)]
struct Plum;

#[derive(PartialEq)]
struct Cherry;

impl PartialEq for Tree {
    fn eq(&self, other: &Tree) -> bool {
        self == other
    }
}

But I'm unable to get it compiled.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
racetrack
  • 3,766
  • 30
  • 30

0 Answers0