error[E0605]: non-primitive cast: `&Arc<Bar>` as `&Arc<dyn Debug>`
9 | &self.bar as &Arc<dyn std::fmt::Debug>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
For more information about this error, try `rustc --explain E0605`.
My instinct says no, but are there any ways to make this work besides either storing an Arc<Dyn Debug> as the bar field and losing the ability to access the concrete type, or modifying the method to return an owned Arc<dyn Debug> and requiring a reference count bump on each access?
I believe the idea behind returning a &Arc<dyn Trait> is so that you have the option to clone the Arc and hang onto the object for longer if you need to.
In the real code, these are fairly heavy weight objects that are useful in their own right, like an abstraction over an async executor, the networking layer, caching, and so on. You could imagine one part of the code using the reference directly in parent.executor().block_on(some_future) (where executor() returns a &Arc<dyn Async Runtime>), while another part of the code clones a handle to your networking layer and sends it to a background thread to perform some operation.