Can/should RefCell implement Eq?


#1

Right now I’m trying to use an enum that looks something like this:

pub enum Types {
    A(bool),
    B(i32),
    C(String),
    D(SomeEnumType),
    E(Rc<RefCell<SomeStruct>>)
}

As the key type for a HashMap, which requires Types to implement Eq. The issue is that all of the types used in Types can easily implement PartialEq and Eq EXCEPT for Rc<RefCell<SomeStruct>>, since RefCell does not implement Eq. It already implements PartialEq.

Is there some particular reason that RefCell can’t implement Eq?

If so, is there a way instead to simply check that two Rc instances point to the same value, and somehow avoid the lack of an Eq implementation?

Edit:
For those that are running into the same problem as me, I figured out a solution. You can create another type that “holds” the Rc type and implements Eq and PartialEq:

pub enum Types {
    ...
    E(StructHolder)
}

#[derive(PartialEq)]
pub struct StructHolder(pub Rc<RefCell<SomeStruct>>);
impl Eq for StructHolder {}

Which then allows you to make a HashMap<Types, _>.


#2

Not sure why it doesn’t impl Eq, but do note that comparisons will panic if the cell is mutably borrowed.