简体   繁体   English

如何通过Rc <refcell<dyn t> > 想要 &dyn T 的 fn? </refcell<dyn>

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

I have trouble passing an argument to a fn.我无法将参数传递给 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() .我认为这就像 rust 书中的示例一样,其中Rc被自动取消引用,为了从 RefCell 中获取值,我可以调用 borrow borrow()

I tried explicit dereferencing too, but nothing seems to work.我也尝试过显式取消引用,但似乎没有任何效果。

How can I call foo() for each dyn T object in self ?如何为self中的每个dyn T object 调用foo()

As the error says, Ref<X> does not automatically implement every trait that X implements.正如错误所说, Ref<X>不会自动实现X实现的每个特征。 For a type to be coerced to a trait object, it needs to implement that trait.对于要强制为特征 object 的类型,它需要实现该特征。

You can explicitly dereference the Ref and then borrow it again:您可以显式取消引用Ref ,然后再次借用它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 Rc <RefCell<dyn T> &gt;&gt; 在子类型上 - Get Rc<RefCell<dyn T>>> on a sub-type 我如何通过Rc <RefCell<Box<MyStruct> &gt;&gt;接受Rc的功能 <RefCell<Box<dyn MyTrait> &gt;&gt;? - How do I pass Rc<RefCell<Box<MyStruct>>> to a function accepting Rc<RefCell<Box<dyn MyTrait>>>? 为什么我不能在 Rc 中使用这个闭包<dyn Fn()> ? - Why can't I use this closure inside a Rc<dyn Fn()>? 如何向下转换 Rc <refcell<dyn io::write> > 变成一个具体的类型? </refcell<dyn> - How to downcast Rc<RefCell<dyn io::Write>> into a concrete type? 如何移动 Vec <box<dyn trait> &gt; 走进 Vec <rc<refcell<dyn trait> &gt;&gt; </rc<refcell<dyn></box<dyn> - How to move a Vec<Box<dyn Trait>> Into Vec<Rc<RefCell<dyn Trait>>> 我如何施放 Rc <refcell<concretetype> &gt; 到 Rc <refcell<dyn trait> &gt;? </refcell<dyn></refcell<concretetype> - How do I cast Rc<RefCell<ConcreteType>> to Rc<RefCell<dyn Trait>>? 向下转换 Rc <refcell<box<struct> &gt;&gt; 到 Rc <refcell<box<dyn trait> &gt;&gt; </refcell<box<dyn></refcell<box<struct> - Downcasting a Rc<RefCell<Box<struct>>> to Rc<RefCell<Box<dyn trait>>> 如何通过&str生活到Box <dyn Fn()> - How to pass through &str lifetime to Box<dyn Fn()> 你如何转换一个盒子<dyn trait>到 Rc<dyn trait> ?</dyn></dyn> - How do you convert a Box<dyn Trait> to a Rc<dyn Trait>? 为什么弧<dyn fn(dyn fnonce(&mut [u8]), usize) -> Result&lt;(), ()&gt; + Send + Sync&gt; 在编译时不知道 syze</dyn> - Why Arc<dyn Fn(dyn FnOnce(&mut [u8]), usize) -> Result<(), ()> + Send + Sync> doesn't have syze known at compile time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM