Shortly, I have a batch of structs implements same trait, and they are upcasted into dyn object and added into a Notifier, and the Notifier will return the Vec<dyn Object> consists of triggered items. But I cannot findout the concrete one. Any maybe useful, but I cannot modify Trait Foo.
Compiling playground v0.0.1 (/playground)
error[E0369]: binary operation `==` cannot be applied to type `&Rc<dyn Source>`
37 | .find(|b| b == &bar1);
| - ^^ ----- &Rc<Bar>
| &Rc<dyn Source>
For more information about this error, try `rustc --explain E0369`.
error: could not compile `playground` due to previous error
Relating this to the code example, you mean you cannot modify the trait Source? If so, can you not modify the Notifier struct either? If there's too many restrictions, you'll quickly be out of luck and the problem can't be solved. Otherwise, please specify your restrictions a bit more clearly, then perhaps adding a method to Source, or using a subtrait in Notifier, either way getting some &dyn Any (or comparable) view of the contained value, would be an approach that should work.
Edit: Only looked at the playground now, that explains what Foo is referring to.
for this specific use of Rc, there's Rc::ptr_eq(), however CoerceUnsized is only implemented for Rc<T>, not for &Rc<T> (in other words, the coersion consumes the original Rc), so you either manually cocerce the Rc<Bar> into Rc<dyn Source> for the closure to capture, or you take a clone of the Rc on each comparison
// manually cocersion outside the closure
// you can make a clone of bar1 if the concrete type is needed later
let target: Rc<dyn Source> = bar1; // bar1.clone();
foo.into_iter().find(|b| Rc::ptr_eq(b, &target));
// make a clone of bar1 each comparison
foo.into_iter().find(|b| Rc::ptr_eq(b, &bar1.clone());
A typo... wanted to say “subtrait ofNotifier”. Something intuitively equivalent to Notifier + Any could be built, let's call it AnyNotifier (for lack of a better name), then the struct could work ist Box<dyn AnyNotifier> but still allows viewing as &dyn Any or &dyn Notifier (using the original not-supposed-to-be-modified trait), whenever necessary.