Rc<RefCell<dyn Trait>> to Rc<RefCell<T>> with std::any::Any

Hi, I have a Rc<RefCell<dyn Trait>> of a trait object and do know which concrete type T the trait object has.
I want to get a &Rc<RefCell<T>> out of the previous, and found std::any::Any, which should to this. Unfortunately, I couldn't find examples with Any and RefCells, and what I've tried doesn't work.
Following (minimal) code example compiles, but panics on the unwrap():

use std::{cell::RefCell, rc::Rc, any::Any};

trait Foo {}
struct Bar {}
impl Foo for Bar {}

fn baz(b: &Rc<RefCell<dyn Foo>>) -> &dyn Any {
    b
}

fn main() {
    let x: Rc<RefCell<dyn Foo>> = Rc::new(RefCell::new(Bar{}));
    let y = baz(&x);
    let _z = y.downcast_ref::<Rc<RefCell<Bar>>>().unwrap();
}

So, I guess, I'm using Any incorrectly. Can anyone give me help, how to use Any here?
BR

This might be what you're looking for: How to downcast Rc<RefCell<dyn Trait>>? - #2 by 2e71828

Thx for pointing me there. That helps a bit further.
If I have a Rc<dyn Any>, i can .downcast it to my Rc<RefCell<T>>, so this works:

use std::{cell::RefCell, rc::Rc, any::Any};

trait Trait {}
struct Struct {}
impl Trait for Struct {}

fn main() {
    let x = Rc::new(RefCell::new(Struct{}));
    let y: Rc<dyn Any> = x;
    let _z = y.downcast::<RefCell<Struct>>().unwrap(); // fine
}

But if I have a Rc<RefCell<dyn Trait>>, I can't make a Rc<dyn Any> out of it, error is
error[E0277]: the size for values of type dyn Trait cannot be known at compilation time

use std::{cell::RefCell, rc::Rc, any::Any};

trait Trait {}
struct Struct {}
impl Trait for Struct {}

fn main() {
    let x: Rc<RefCell<dyn Trait>> = Rc::new(RefCell::new(Struct{}));
    let y: Rc<dyn Any> = x; // error here
    let _z = y.downcast::<RefCell<Struct>>().unwrap();
}

is there some way to go from Rc<RefCell<dyn Trait>> to Rc<dyn Any>?

Yes, but you have to implement it for Trait. @DanielKeep wrote a great answer to your question on SO:

thx for the link.
there are Box<dyn Trait> items used, Box is equivallent to Rc in my example, but I have an additional layer of RefCell.
Maybe it's obvious, but I don't see how this works with RefCells...
Can you give me further advice please?
BR

If you want to downcast to RefCell<Struct>, don't use dyn Trait, just coerce Rc<RefCell<Struct>> to Rc<dyn Any>.

    let x: Rc<RefCell<Struct>> = Rc::new(RefCell::new(Struct{}));
    let y: Rc<dyn Any> = x;
    let _z = y.downcast::<RefCell<Struct>>().unwrap();

If you want to downcast to RefCell<dyn Trait>, then you'll need an extra layer of indirection because you can't unsize-coerce something with dynamic size (RefCell<dyn Trait>) to dyn Any.

    let x: Rc<Box<RefCell<dyn Trait>>> = Rc::new(Box::new(RefCell::new(Struct{})));
    let y: Rc<dyn Any> = x;
    let _z = y.downcast::<Box<RefCell<dyn Trait>>>().unwrap();

ok, thx. that's still not what i actually wanted.
So I guess, my small example/my questions aren't describing my problem properly, so I post my larger problem, that lead me to this question:
I actually wanted to try, if I can make the rand::thread_rng() (thread_rng in rand - Rust) function generic. After looking at the original source, and with the previous answers to this thread, this is what i have:
Rust Playground

It is working like it is, but somehow I dislike the UnsafeCell<HashMap<TypeId, Rc<dyn Any>>>
and would actually prefer something like UnsafeCell<HashMap<TypeId, Rc<UnsafeCell<dyn RngCore>>>
but i can't get it working that way. Is it somehow possible to make the hashmap members look more like the actuall thing they are storing?
The problem is line 20, where I can't get the proper Rc type to clone and put in the struct.

You would need RngCore to supply downcasting support (assuming you still want to downcast). Or you could make a subtrait and supply it yourself; I haven't written up an example of doing this, but refer to how Error does it.

I probably wouldn't bother if this is just an unexposed implementation detail and what you have works, but anyway, I believe that's a way to do it.