Compare objects identified by trait

I would like to compare objects which are identified by a trait. Like in the following example :

trait Component: PartialEq {}

struct MyStruct {}

impl MyStruct {
    fn find_component(&self) -> Option<Box<dyn Component>> {

pub fn main() {
    let a = MyStruct {};
    let b = MyStruct {};

    let is_same = a.find_component() == b.find_component();

But it seems not allowed :

   Compiling demo v0.1.0 (/home/bastiensevajol/Projets/demo)
error[E0038]: the trait `Component` cannot be made into an object
 --> src/
6 |     fn find_component(&self) -> Option<Box<dyn Component>> {
  |                                            ^^^^^^^^^^^^^ `Component` cannot be made into an object
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <>
 --> src/
1 | trait Component: PartialEq {}
  |       ---------  ^^^^^^^^^ ...because it uses `Self` as a type parameter
  |       |
  |       this trait cannot be made into an object...

For more information about this error, try `rustc --explain E0038`.
error: could not compile `demo` due to previous error

How can I compare objects which are identified by a trait ? Thanks !

You can do that with the dyn_eq crate.


I also created dyn_ord for equality and in general, ordering comparisons on trait objects.

(It also looks like it has the added advantage of not requiring a separate macro invocation due to the provided blanket PartialEq impl on the trait object type itself.)


Thanks! I will try these different solutions!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.