rustdereferencetrait-objectsinterior-mutability

How to pass Rc<RefCell<dyn T>> to fn that wants &dyn T?


I have trouble passing an argument to a fn.

trait T {}

struct S {
    others: Vec<Rc<RefCell<dyn T>>>
}

impl S {
    fn bar(&self) {
        for o in self.others {
            foo(&o.borrow());
        }
    }
}

fn foo(t: &dyn T) {}

The compiler tells me:

error[E0277]: the trait bound `std::cell::Ref<'_, (dyn T + 'static)>: T` is not satisfied
  --> src/lib.rs:14:17
   |
14 |             foo(&o.borrow());
   |                 ^^^^^^^^^^^ the trait `T` is not implemented for `std::cell::Ref<'_, (dyn T + 'static)>`
   |
   = note: required for the cast to the object type `dyn T`

I thought this was like in the example in the rust book, where the Rc is auto dereferenced and to get the value out of the RefCell I could call borrow().

I tried explicit dereferencing too, but nothing seems to work.

How can I call foo() for each dyn T object in self?


Solution

  • As the error says, Ref<X> does not automatically implement every trait that X implements. For a type to be coerced to a trait object, it needs to implement that trait.

    You can explicitly dereference the Ref and then borrow it again:

    impl S {
        fn bar(&self) {
            for o in &self.others {
                foo(&*o.borrow());
            }
        }
    }