我有一个结构 A
具有盒装特征( Foo
),另一个结构 BoxedA
它有一个 Rc<RefCell<A>>
在里面。我正在尝试在 BoxedA
上创建一个方法返回对盒装特征的引用,但在映射 Ref<A>
时一直遇到生命周期问题至 Ref<Foo>
.
这是我的代码:
use std::rc::Rc;
use std::cell::{RefCell, Ref};
trait Foo {
}
struct A {
a: Box<Foo>
}
impl A {
fn new(a: Box<Foo>) -> A {
A { a: a }
}
fn a(&self) -> &Foo {
&*self.a
}
}
struct BoxedA {
a: Rc<RefCell<A>>
}
impl BoxedA {
fn new(a: Box<Foo>) -> BoxedA {
BoxedA {
a: Rc::new(RefCell::new(A::new(a)))
}
}
fn a(&self) -> Ref<Foo> {
Ref::map(self.a.borrow(), |a| a.a())
}
}
impl Foo for i32 {
}
fn main() {
let a = BoxedA::new(Box::new(3));
let a_ref = a.a();
}
Rust Playground 链接:https://play.rust-lang.org/?gist=d0348ad9b06a152770f3877864b01531&version=stable&backtrace=0
我得到以下编译错误:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> <anon>:34:41
|
34 | Ref::map(self.a.borrow(), |a| a.a())
| ^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 34:38...
--> <anon>:34:39
|
34 | Ref::map(self.a.borrow(), |a| a.a())
| ^^^^^
note: ...so that reference does not outlive borrowed content
--> <anon>:34:39
|
34 | Ref::map(self.a.borrow(), |a| a.a())
| ^
note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the body at 33:28...
--> <anon>:33:29
|
33 | fn a(&self) -> Ref<Foo> {
| _____________________________^ starting here...
34 | | Ref::map(self.a.borrow(), |a| a.a())
35 | | }
| |_____^ ...ending here
note: ...so that the declared lifetime parameter bounds are satisfied
--> <anon>:34:9
|
34 | Ref::map(self.a.borrow(), |a| a.a())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
奇怪的是,如果我替换所有 Foo
代码编译与i32
.
最佳答案
问题出在这个签名上:
fn a(&self) -> &Foo { ... }
根据生命周期省略规则,这扩展为:
fn a<'b>(&'b self) -> &'b (Foo + 'b) { ... }
哇,那是什么+ 'b
位?
Trait 对象有一个生命周期限制,指定对象中包含的引用的最短生命周期。如果该类型不包含任何引用,则该生命周期绑定(bind)将为 'static
.
如果您在包含引用的类型上实现特征,例如一对引用:
impl<'a, 'b> Foo for (&'a u32, &'b u32) {}
那么你有一个 &Foo
类型的变量这恰好是对 (&'a u32, &'b u32)
的引用(要清楚,这是对一对引用的引用),关于这两个 &u32
的生命周期信息在哪里?引用去?这就是 trait 对象上生命周期的用武之地。 &Foo
的完整扩展类型看起来像&'c (Foo + 'd)
, 其中'd
是'a
中最短的一个和 'b
(或者可能更短,多亏了协方差)。
还有许多其他地方您没有明确指定生命周期限制。所有期望函数返回类型的地方都默认为'static
终生束缚。例如,Box<Foo>
就是这样。在struct A
: 它实际上被解释为 Box<Foo + 'static>
.
您的问题的简单解决方案是指定 A::a
返回带有 'static
的特征对象生命周期限制:
fn a(&self) -> &(Foo + 'static) { ... }
这很有意义,因为我们返回一个指向 Box<Foo + 'static>
内部的指针!
BoxedA::a
最终可能会给您带来类似的问题,因此您可能也想在解决这个问题的同时解决这个问题:
fn a(&self) -> Ref<Foo + 'static> { ... }
既然您已经了解了这些生命周期边界,您可能想考虑将 A
设为是否有意义和 BoxedA
在该生命周期范围内通用,而不是强制执行 'static
.如果您想最大化通用性,您的代码将如下所示:
struct A<'a> {
a: Box<Foo + 'a>
}
impl<'a> A<'a> {
fn new(a: Box<Foo + 'a>) -> A<'a> {
A { a: a }
}
fn a(&self) -> &(Foo + 'a) {
&*self.a
}
}
struct BoxedA<'a> {
a: Rc<RefCell<A<'a>>>
}
impl<'a> BoxedA<'a> {
fn new(a: Box<Foo + 'a>) -> BoxedA<'a> {
BoxedA {
a: Rc::new(RefCell::new(A::new(a)))
}
}
fn a(&self) -> Ref<Foo + 'a> {
Ref::map(self.a.borrow(), |a| a.a())
}
}
是否需要这种级别的通用性由您决定。
关于rust - 将 Ref::map 与特征对象一起使用时无法推断生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42541298/