3

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?

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
David
  • 1,672
  • 2
  • 13
  • 32

1 Answers1

5

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());
        }
    }
}
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
  • When I use this, I get a similar compiler error, saying Borrow is not implemented for Rc. – David Feb 11 '20 at 12:59
  • I don't understand why (in the original question) the compiler complained about the borrow() call. I mean RefCell does have a borrow method, even though it does not implement the Borrow trait. Why is the RefCell not auto dereferenced out of the Rc so I can call borrow on the RefCell? – David Feb 11 '20 at 13:10
  • Yet, the solution works on the [playground](https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=dcc273be130b8613f3729127f4cab369) – Jmb Feb 11 '20 at 13:13
  • Ah I had Borrow in use, that's why duuhh! – David Feb 11 '20 at 13:29
  • 3
    @David If you do need `Borrow` as well, you can use the `RefCell` one explicitly: `foo(&*RefCell::borrow(&o))` – trent Feb 11 '20 at 14:12