Can/should RefCell implement Eq?


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

pub enum Types {

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?

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 {

pub struct StructHolder(pub Rc<RefCell<SomeStruct>>);
impl Eq for StructHolder {}

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


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