如何将Rc<RefCell<dyn T>>传递给需要&dyn T的函数?

3

我在传递参数给函数时遇到了问题。

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) {}

编译器告诉我:
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`

我认为这就像rust书中的示例,其中Rc自动解引用,要从RefCell中获取值,可以调用borrow()
我也尝试了显式解引用,但是似乎没有用。
如何在self中为每个dyn T对象调用foo()
1个回答

5

正如错误所述,Ref<X>并不会自动实现 X 实现的每个 trait。要将类型强制转换为 trait 对象,它需要实现该 trait。

您可以显式地取消引用 Ref,然后再次借用它:

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

当我使用这个时,我会得到一个类似的编译器错误,说Rc<Refcell>没有实现Borrow。 - David
我不明白为什么(在原始问题中)编译器会抱怨borrow()的调用。我的意思是RefCell确实有一个借用方法,即使它没有实现Borrow特性。为什么RefCell没有自动从Rc中解除引用,以便我可以在RefCell上调用borrow呢? - David
然而,该解决方案在playground上运行正常。 - Jmb
啊,我之前已经使用了Borrow,所以才会出现这种情况! - David
3
@David 如果您需要同时使用 Borrow,您可以显式地使用 RefCell 版本:foo(&*RefCell::borrow(&o)) - trent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接