I have a trait MyTrait
and a struct MyStruct
that implements MyTrait
.
I also have a function that accepts Rc<RefCell<Box<MyTrait>>>
as an argument.
Somewhere in the code I create an instance of Rc<RefCell<Box<MyStruct>>>
:
let my_struct = Rc::new(RefCell::new(Box::new(MyStruct)));
When I pass my_struct
to my function I get a compiler error:
error: mismatched types:
expected alloc::rc::Rc<core::cell::RefCell<Box<MyTrait>>>
,
found alloc::rc::Rc<core::cell::RefCell<Box<MyStruct>>>
I try to fix that by creating an instance of Rc<RefCell<Box<MyStruct>>>
by explicitly specifying the type I need:
let my_struct: Rc<RefCell<Box<MyTrait>>> = Rc::new(RefCell::new(Box::new(MyStruct)));
In this case passing my_struct
to my function works fine, however I can't access any MyStruct
specific fields via my_struct
variable anymore. And it doesn't seem to be a way to cast down Rc<RefCell<Box<MyTrait>>>
to Rc<RefCell<Box<MyStruct>>>
.
How can I go around this problem?