Poperton
Poperton

Reputation: 2127

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

I'm implementing a trait for 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

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

move occurs because *self has type phy::virtual_tun::VirtualTapInterface, which does not implement the Copy traitrustc(E0507)

How is it possible to create a Rc<RefCell<>> of a self mutable reference?

Upvotes: 3

Views: 1758

Answers (1)

Solomon Ucko
Solomon Ucko

Reputation: 6109

I think you need to change the signature to fn receive(self) -> ... to take ownership. Cloning or taking a box could also work.

Another option is to use mem::take, mem::replace, or mem::swap.

Upvotes: 2

Related Questions