Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert boxed trait to mutable trait reference in Rust

Tags:

rust

I am having some trouble with the dynamic dispatch pointer types in Rust. I want to convert a value of type Box<MyTrait> to &mut MyTrait to pass to a function. For example, I tried:

use std::borrow::BorrowMut;

trait MyTrait {
    fn say_hi(&mut self);
}

struct MyStruct { }

impl MyTrait for MyStruct {
    fn say_hi(&mut self) {
        println!("hi");
    }
}

fn invoke_from_ref(value: &mut MyTrait) {
    value.say_hi();
}

fn main() {
    let mut boxed_trait: Box<MyTrait> = Box::new(MyStruct {});
    invoke_from_ref(boxed_trait.borrow_mut());
}

This fails with the following error:

error: `boxed_trait` does not live long enough
  --> <anon>:22:5
   |
21 |         invoke_from_ref(boxed_trait.borrow_mut());
   |                         ----------- borrow occurs here
22 |     }
   |     ^ `boxed_trait` dropped here while still borrowed
   |
   = note: values in a scope are dropped in the opposite order they are created

Strangely enough, this works for &MyTrait but not for &mut MyTrait. Is there any way I can get this conversion to work in the mutable case?

like image 821
Iceberg Avatar asked Jan 09 '17 04:01

Iceberg


1 Answers

I think you're running into a limitation of the current compiler's lifetime handling. borrow_mut, being a function, imposes stricter lifetime requirements than necessary.

Instead, you can take a mutable borrow to the box's interior by first dereferencing the box, like this:

fn main() {
    let mut boxed_trait: Box<MyTrait> = Box::new(MyStruct {});
    invoke_from_ref(&mut *boxed_trait);
}
like image 157
Francis Gagné Avatar answered Oct 12 '22 23:10

Francis Gagné