我在Rust中使用动态调度指针类型时遇到了一些问题.我想类型的值转换Box<MyTrait>到&mut MyTrait传递给函数.例如,我尝试过:
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());
}
Run Code Online (Sandbox Code Playgroud)
此操作失败,并显示以下错误:
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
Run Code Online (Sandbox Code Playgroud)
奇怪的是,这适用&MyTrait但不适用&mut MyTrait.有什么办法可以让这个转换在可变的情况下工作吗?
我认为你遇到了当前编译器生命周期处理的限制.borrow_mut作为一种功能,强加了比必要的更严格的寿命要求.
相反,您可以通过首先解除引用框来对盒子的内部进行可变借用,如下所示:
fn main() {
let mut boxed_trait: Box<MyTrait> = Box::new(MyStruct {});
invoke_from_ref(&mut *boxed_trait);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
433 次 |
| 最近记录: |