这个playground project有一个简化版本的代码我试图编译。
use std::sync::{Arc, Mutex, MutexGuard};
pub trait Runnable {
fn run(&mut self) -> Option<String>;
}
pub struct Value {}
impl Runnable for Value {
fn run(&mut self) -> Option<String> {
Some("Value".to_string())
}
}
pub struct RunList {
runnables: Vec<Arc<Mutex<Runnable>>>,
}
impl RunList {
pub fn run<R>(&mut self, index: usize, mut runner: R)
where
R: FnMut(&mut MutexGuard<Runnable>),
{
let runnable_arc = self.runnables[index].clone();
let mut runnable = runnable_arc.lock().unwrap();
runner(&mut runnable);
}
}
fn main() {
let mut runnables = Vec::<Arc<Mutex<Runnable>>>::with_capacity(1);
runnables.push(Arc::new(Mutex::new(Value {})));
let mut run_list = RunList { runnables };
run_list.run(0, |runnable| {
println!("Hello, {}", runnable.run().unwrap());
});
}
我想要一个特征对象的向量,其中每个对象都受Arc
和Mutex
的保护,然后能够在每个对象上调用特征方法。
我有一个&#34;借来的价值不够长的时间&#34;错误,但我看不出与this question/answer的区别。
error[E0597]: `runnable_arc` does not live long enough
--> src/main.rs:25:28
|
25 | let mut runnable = runnable_arc.lock().unwrap();
| ^^^^^^^^^^^^ borrowed value does not live long enough
26 | runner(&mut runnable);
27 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
答案 0 :(得分:3)
您似乎在编译器的推理系统中遇到了一个小故障。将MutexGuard<Runnable>
更改为MutexGuard<Runnable + 'static>
可修复错误:
impl RunList {
pub fn run<R>(&mut self, index: usize, mut runner: R)
where
R: FnMut(&mut MutexGuard<Runnable + 'static>),
{
let runnable_arc = self.runnables[index].clone();
let mut runnable = runnable_arc.lock().unwrap();
runner(&mut runnable);
}
}
但有趣的是,将其更改为MutexGuard<'static, Runnable + 'static>
会保留错误:
impl RunList {
pub fn run<R>(&mut self, index: usize, mut runner: R)
where
R: FnMut(&mut MutexGuard<'static, Runnable + 'static>),
{
let runnable_arc = self.runnables[index].clone();
let mut runnable = runnable_arc.lock().unwrap();
runner(&mut runnable);
}
}
为什么编译器会推断后者而不是前者呢?好吧,让我们来看看MutexGuard
的定义:
pub struct MutexGuard<'a, T: ?Sized + 'a> { /* fields omitted */ }
在MutexGuard<Runnable>
中,Runnable
是一个特征对象并获得一个隐式+ 'static
绑定,因为没有明确的生命周期。似乎编译器试图通过推断,如果我们需要用'a
替换第二次出现来统一'static
的两次出现,那么我们需要对第一次出现做同样的事情。但是,该逻辑会忘记一个重要的细节:'static
超过任何生命周期'a
,因此没有理由强迫第一个'a
成为'static
。实际上,当我们写MutexGuard<Runnable + 'static>
时,它与MutexGuard<'a, Runnable + 'static>
相同(如何定义'a
取决于上下文)。这是有效的,因为Runnable + 'static: 'a
(您始终可以将具有生命周期'x
的对象作为参数传递给期望更短生命周期'y
的函数)。
记录中,该问题与FnMut
特征无关;正常函数也会显示此行为:
impl RunList {
pub fn run(&mut self, index: usize) {
fn runner2(runnable: &mut MutexGuard<Runnable>) {
println!("Hello, {}", runnable.run().unwrap());
}
let runnable_arc = self.runnables[index].clone();
let mut runnable = runnable_arc.lock().unwrap();
runner2(&mut runnable);
}
}
答案 1 :(得分:1)
您正在将MutexGuard
的引用传递给runner
,而不是对包装的runnable的引用。您需要插入deref:
pub fn run<R>(&mut self, index: usize, mut runner: R)
where
R: FnMut(&mut Runnable),
{
let runnable_arc = self.runnables[index].clone();
let mut runnable = runnable_arc.lock().unwrap();
runner(&mut *runnable);
}
根据我的理解,这个构造 - &mut *
- 在这里不是必需的,因为MutexGuard
实现了DerefMut
特征。显然,这是一个已知问题:Why does a mutable borrow of a closure through DerefMut not work?