简体   繁体   中英

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 ?

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());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM