Hello,
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>> {
None
}
}
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/main.rs:6:44
|
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 <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> src/main.rs:1:18
|
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 !