简体   繁体   English

如何创建一个 Rc <refcell<> &gt; 自可变引用? </refcell<>

[英]How to create a Rc<RefCell<>> of a self mutable reference?

I'm implementing a trait for VirtualTapInterface .我正在为VirtualTapInterface实现一个特征。 The receive function of this trait should create a TxToken struct, where the lower property must be an Rc<RefCell<VirtualTapInterface>> containing the current VirtualTapInterface , that is self此 trait 的receive function 应该创建一个TxToken结构,其中lower属性必须是包含当前VirtualTapInterfaceRc<RefCell<VirtualTapInterface>> ,即self

impl<'a> Device<'a> for VirtualTapInterface {
    type TxToken = TxToken;

    fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
                let tx = TxToken { lower: Rc::new(RefCell::new(*self))};

I tried this but I get that我试过这个,但我明白了

cannot move out of *self which is behind a mutable reference不能移出可变引用后面的*self

move occurs because *self has type phy::virtual_tun::VirtualTapInterface , which does not implement the Copy traitrustc(E0507) move 发生是因为*self具有类型phy::virtual_tun::VirtualTapInterface ,它没有实现Copy traitrustc(E0507)

How is it possible to create a Rc<RefCell<>> of a self mutable reference?如何创建一个自可变引用的Rc<RefCell<>>

I think you need to change the signature to fn receive(self) ->... to take ownership.我认为您需要将签名更改为fn receive(self) ->...以获得所有权。 Cloning or taking a box could also work.克隆或拿一个盒子也可以。

Another option is to use mem::take , mem::replace , or mem::swap .另一种选择是使用mem::takemem::replacemem::swap

如何创建 Rc <refcell< to something that already exists?< div><div id="text_translate"><p> 因此,为了自学一些 Rust,我决定创建一个我喜欢的简单混合数据类型 ChainVec。 这是一个向量的双向链接列表,如果您需要调整可能增长到巨大规模的向量的大小,这很有用。 它使用链表逻辑,但查找、搜索和迭代特性要快得多,因为它们的增长时间为 O(n/alot)。 不过,这并不是重点:我遇到的问题会出现在任何双向链表中。</p><p> 这就是问题所在......在 C 中,当我创建新的链表节点时,我可以将其“尾部”设置为指向前一个节点的指针。 我可以在 C 中像糖果一样扔指针,但在 Rust 中,我还不知道如何创建新指针。 这是我尝试过的...(在 fn push_head 下查找无效行)</p><p> 注意:在我的实现中,chainvec 的“头”节点是您选择指向的任何一个,因此列表头/尾没有单独的结构。</p><pre> #[allow(non_snake_case)] use std::{cell::RefCell, rc::Rc}; #[allow(dead_code)] struct ChainVec&lt;T&gt; { item: Option&lt;Vec&lt;T&gt;&gt;, head: Option&lt;Rc&lt;RefCell&lt;ChainVec&lt;T&gt;&gt;&gt;&gt;, tail: Option&lt;Rc&lt;RefCell&lt;ChainVec&lt;T&gt;&gt;&gt;&gt;, } impl&lt;T&gt; ChainVec&lt;T&gt; { fn new(prealloc: usize) -&gt; Self { let vector: Vec&lt;T&gt; = Vec::with_capacity(prealloc); Self { item: Some(vector), head: None, tail: None, } } fn new_with_links(prealloc: usize, head: Option&lt;Rc&lt;RefCell&lt;ChainVec&lt;T&gt;&gt;&gt;&gt;, tail: Option&lt;Rc&lt;RefCell&lt;ChainVec&lt;T&gt;&gt;&gt;&gt;) -&gt; Self { let vector: Vec&lt;T&gt; = Vec::with_capacity(prealloc); Self { item: Some(vector), head: head, tail: tail, } } fn push_head(&amp;mut self, prealloc: usize) { self.head = Some(Rc::new(RefCell::new(ChainVec::new_with_links(prealloc, None, Some(Rc::new(RefCell::new(self))))))); } } #[allow(unused_variables)] fn main() { let alef: ChainVec&lt;u32&gt; = ChainVec::new(100); println,("Hello; world!"); }</pre><p> 显然,当我尝试通过 Some(Rc::new(RefCell::new(self))) 在 push_head 中创建一个新指针时,我迷路了。 我有</p><pre>mismatched types expected struct `ChainVec&lt;T&gt;` found mutable reference `&amp;mut ChainVec&lt;T&gt;`</pre><p> 取消引用运算符在这里不做任何事情......我不知道如何继续。</p><p> 如何创建对已经存在的结构的 Rc&lt;RefCell&lt; 引用? </p></div></refcell<> - How can I create an Rc<RefCell< to something that already exists?

暂无
暂无

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

相关问题 如何遍历 Rc <refcell<t> &gt; 返回原始可变引用</refcell<t> - How to iterate over an Rc<RefCell<T>> that returns raw mutable references Rust:如何返回对 Rc 的引用<RefCell<HashMap<K, V> &gt; 价值? - Rust: How to return a reference to an Rc<RefCell<HashMap<K, V>> value? 如何从 Rc 获得 &amp;A 参考<RefCell<A> &gt;? - How can I obtain an &A reference from a Rc<RefCell<A>>? 如何创建 Rc <refcell< to something that already exists?< div><div id="text_translate"><p> 因此,为了自学一些 Rust,我决定创建一个我喜欢的简单混合数据类型 ChainVec。 这是一个向量的双向链接列表,如果您需要调整可能增长到巨大规模的向量的大小,这很有用。 它使用链表逻辑,但查找、搜索和迭代特性要快得多,因为它们的增长时间为 O(n/alot)。 不过,这并不是重点:我遇到的问题会出现在任何双向链表中。</p><p> 这就是问题所在......在 C 中,当我创建新的链表节点时,我可以将其“尾部”设置为指向前一个节点的指针。 我可以在 C 中像糖果一样扔指针,但在 Rust 中,我还不知道如何创建新指针。 这是我尝试过的...(在 fn push_head 下查找无效行)</p><p> 注意:在我的实现中,chainvec 的“头”节点是您选择指向的任何一个,因此列表头/尾没有单独的结构。</p><pre> #[allow(non_snake_case)] use std::{cell::RefCell, rc::Rc}; #[allow(dead_code)] struct ChainVec&lt;T&gt; { item: Option&lt;Vec&lt;T&gt;&gt;, head: Option&lt;Rc&lt;RefCell&lt;ChainVec&lt;T&gt;&gt;&gt;&gt;, tail: Option&lt;Rc&lt;RefCell&lt;ChainVec&lt;T&gt;&gt;&gt;&gt;, } impl&lt;T&gt; ChainVec&lt;T&gt; { fn new(prealloc: usize) -&gt; Self { let vector: Vec&lt;T&gt; = Vec::with_capacity(prealloc); Self { item: Some(vector), head: None, tail: None, } } fn new_with_links(prealloc: usize, head: Option&lt;Rc&lt;RefCell&lt;ChainVec&lt;T&gt;&gt;&gt;&gt;, tail: Option&lt;Rc&lt;RefCell&lt;ChainVec&lt;T&gt;&gt;&gt;&gt;) -&gt; Self { let vector: Vec&lt;T&gt; = Vec::with_capacity(prealloc); Self { item: Some(vector), head: head, tail: tail, } } fn push_head(&amp;mut self, prealloc: usize) { self.head = Some(Rc::new(RefCell::new(ChainVec::new_with_links(prealloc, None, Some(Rc::new(RefCell::new(self))))))); } } #[allow(unused_variables)] fn main() { let alef: ChainVec&lt;u32&gt; = ChainVec::new(100); println,("Hello; world!"); }</pre><p> 显然,当我尝试通过 Some(Rc::new(RefCell::new(self))) 在 push_head 中创建一个新指针时,我迷路了。 我有</p><pre>mismatched types expected struct `ChainVec&lt;T&gt;` found mutable reference `&amp;mut ChainVec&lt;T&gt;`</pre><p> 取消引用运算符在这里不做任何事情......我不知道如何继续。</p><p> 如何创建对已经存在的结构的 Rc&lt;RefCell&lt; 引用? </p></div></refcell<> - How can I create an Rc<RefCell< to something that already exists? Rust 运行时可变引用规则与 RefCell - Rust runtime mutable reference rules with RefCell 如何获得 rc::Ref<t> 对 rc::Weak 指向的节点的引用<refcell<t> >? </refcell<t></t> - How to get a rc::Ref<T> reference to a node pointed by a rc::Weak<RefCell<T>>? 如何将可变自引用传递给特征方法? - How to pass a mutable self reference on to a trait method? 如何在 rust(和 stdweb)中使用 Rc Refcell - How use Rc Refcell in rust (and stdweb) 如何获得 RefCell 到一个对象和 Rc 到它的一部分 - How to get RefCell to an object and Rc to its part 如何创建捕获 Rc 的 wasm_bindgen 闭包<RefCell<_> &gt; 不搬出去? - How can I create a wasm_bindgen Closure which captures a Rc<RefCell<_>> without moving out of it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM