Rc<RefCell<dyn Any>> to Rc<RefCell<ConcreteType>>: What are the reasons it is not done in the std crate and how can we about doing it?

How to go from Rc<RefCell<dyn Any>> to Rc<RefCell<ConcreteType>> ?

From the rust documentation there is NO

impl Rc<RefCell<dyn Any>> { 
    fn downcast<T: Any>(self) -> Rc<RefCell<ConcreteType>> { 

for the cell family with dyn Any.

There is however

impl Rc<dyn Any> { ... }

as seen from the rust docs.

  • Is this use case intentionally left out by the rust std crate ? If so why (what limitations were faced) ? How can we get around this ?
  • This equally applies to all forms of std::cell like:
    • Rc<Cell<dyn Any>> TO Rc<Cell<ConcreteType>>
    • Rc<RefCell<dyn Any>> TO Rc<Cell<ConcreteType>>
    • Rc<OnceCell<dyn Any>> TO Rc<OnceCell<ConcreteType>>

Is this because there is no provision in rust to detect that there can be multiple owners where one can point to a trait object while the other pointing to a concrete type for the same trait object: The cell controlling the interior mutability is unable to guarantee the rust borrow rules between the trait object and the corresponding concrete object ? If this is the reason is this not a limitation of the rust type system and the core language implementation ? I get that the concrete type has been erased to dyn Any, but still there must be a way to establish down-casting with interior mutability turned on.

A workaround:

I suspect the limitation has something to do with the discussion here, but that's just a guess.


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.