#Why is this trait not `dyn` compatible?

2 messages · Page 1 of 1 (latest)

half path
#
pub trait Val: Any + Eq {}

For context, I have some code that works as I expect that makes use of an Arc<dyn Any>. However, I need to add the further constraint that I can use == for equality. Naively adding Arc<dyn Any + Eq> complains:

only auto traits can be used as additional traits in a trait object
consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Eq + Any {}`

Thus, I created Val and ran into the above error.

Thank you.

misty forge
#

You probably want something like this (haven't really checked how viable this is but it is dyn-compatible so it's a start)

pub trait Val: Any + for<'a> PartialEq<&'a dyn Any> {}
```You'll need to implement `PartialEq<&dyn Any>` for your types by attempting downcasting.