为什么 &'a Box<Trait> 被视为 &'a Box<Trait + 'static> 而不是 &'a Box<Trait + 'a>?

4

考虑下面这段代码:

trait Trait {}
struct Child;
impl Trait for Child {}
struct Father<'a> {
    child: &'a Box<dyn Trait>,
}
impl<'a> Trait for Father<'a> {}

fn main() {
    let child: Box<dyn Trait> = Box::new(Child {});
    let father: Box<dyn Trait> = Box::new(Father { child: &child });
    let grandf: Box<dyn Trait> = Box::new(Father { child: &father });
}

该代码无法在Rust 1.30.0上编译,我收到了以下错误:

error[E0597]: `child` does not live long enough
  --> src/main.rs:11:60
   |
11 |     let father: Box<dyn Trait> = Box::new(Father { child: &child });
   |                                                            ^^^^^ borrowed value does not live long enough
12 |     let grandf: Box<dyn Trait> = Box::new(Father { child: &father });
13 | }
   | - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

我可以通过使用child: &'a Box<dyn Trait + 'a>使代码编译,但我不明白为什么会这样。根据RFC 0599,默认对象约束规则应将类型&'a Box<Trait>视为&'a Box<Trait + 'a>。但实际上,它似乎是&'a Box<Trait + 'static>
以下是需要回答的问题:
  1. 为什么我的原始代码无法编译?
  2. 默认对象绑定是否像看起来那样采用了&'a Box<Trait + 'static>
为什么需要使用加号运算符(Iterator<Item = &Foo> + 'a)将生命周期添加到trait中?这个问题有一个关键区别。根据RFC 0599中在该问题的答案中提到的内容,&'a Box<SomeTrait>类型和仅有Box<SomeTrait>之间存在不同,使它们具有不同的默认生命周期。因此,在这种情况下,根据RFC,我认为盒装特征的默认生命周期应为'a而不是'static
这意味着要么有一个更新的RFC改变了RFC 0599的规定,要么还有另一个原因导致这段代码无法工作。在这两种情况下,那个问题的答案都不适用于这个问题,因此,这不是一个重复的问题。
1个回答

3
这些规则已经通过 RFC 1156 进行了调整(重点是我的):
调整对象默认绑定算法,以处理像 &'x Box<Trait>&'x Arc<Trait> 这样的情况。现有算法会默认为 &'x Box<Trait+'x>。建议的更改是默认为 &'x Box<Trait+'static>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接