[英]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
属性必须是包含当前VirtualTapInterface
的Rc<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 typephy::virtual_tun::VirtualTapInterface
, which does not implement theCopy
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::take
、 mem::replace
或mem::swap
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.