我有一个普通的Interface
号特征.Interface
有一个method
,它将使用动态调度来调用.具体地说,我将在Rc<RefCell<dyn Interface>>
岁左右通过.
我有一个用例,其中我需要将对method
的调用包装在闭包中.所需的行为是使用input
调用闭包将与使用input
调用obj.method
相同,其中obj
实现Interface
.
以下是我到目前为止的try .
use std::cell::RefCell;
use std::rc::Rc;
pub trait Interface<'p, T> {
fn method<'s: 'p>(&'p self, input: &'s str) -> T;
}
fn wrap_interface_with_closure<'p, 's: 'p, T: 'p>(
instance: Rc<RefCell<dyn Interface<'p, T>>>,
) -> impl FnMut(&'s str) -> T + 'p {
move |input| (*instance).borrow().method(input)
}
这会给我带来以下错误:
error[E0716]: temporary value dropped while borrowed
|
13 | instance: Rc<RefCell<dyn Interface<'p, T>>>,
| -------- lifetime `'2` appears in the type of `instance`
14 | ) -> impl FnMut(&'s str) -> T + 'p {
15 | move |input| (*instance).borrow().method(input)
| ^^^^^^^^^^^^^^^^^^^^--------------
| | |
| | temporary value is freed at the end of this statement
| creates a temporary which is freed while still in use
| argument requires that borrow lasts for `'2`
error: lifetime may not live long enough
|
13 | instance: Rc<RefCell<dyn Interface<'p, T>>>,
| -------- lifetime `'2` appears in the type of `instance`
14 | ) -> impl FnMut(&'s str) -> T + 'p {
15 | move |input| (*instance).borrow().method(input)
| ------------ ^^^^^^^^ closure capture requires that `'1` must outlive `'2`
| |
| lifetime `'1` represents this closure's body
|
= note: closure implements `FnMut`, so references to captured variables can't escape the closure
error: aborting due to 2 previous errors; 2 warnings emitted
当它说closure implements FnMut, so references to captured variables can't escape the closure
时,它指的是哪个捕获的变量?是不是说instance
号逃走了?这是否意味着借款正在逃逸?