结构持有的特征对象中的显式生命周期声明 [英] Explicit lifetime declarations in trait objects held by structs

查看:70
本文介绍了结构持有的特征对象中的显式生命周期声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的答案中,有一个关于如何引用结构所包含的特征对象,它需要以下语法:

In the answer to this question there's a discussion of how to refer to trait objects held by structs which requires the following syntax:

struct Bar<'a> {
    foo: &'a (Foo + 'a),
}

这是根据 RFC 438

我可以要求对双重寿命声明进行更多的解释吗?莱万斯说:

Could I ask for more explanation of the double lifetime declaration? Levans said:

您必须指定两次生存期:一次用于 引用,并且一次用于特质对象本身,因为特质 可以为引用实现,并且如果基础对象是 参考,还必须指定其生存期.

You have to specify the lifetime two times : once for the lifetime of the reference, and once for the trait object itself, because traits can be implemented for references, and if the underlying object is a reference, you must specify its lifetime as well.

我了解结构中引用的生存期概念.但是我不明白为什么生命不是针对特质是特质的对象.换句话说,我不知道持有一个特征的引用而不持有对该特征的潜在事物的引用是什么意思.

I understand the notion of a lifetime for the reference in the struct. But I don't understand why the lifetime isn't for the object that the trait is a trait on. Put another way, I don't know what it means to hold a reference for a trait without holding a reference to the underlying thing that it's a trait for.

是否存在特征和基础对象具有不同生存期的情况?保留对特征的引用而不保留特征所处的基础是什么意思?

Is there a case where the trait and the underlying object would have different lifetimes? What would it mean to hold onto a reference to a trait without holding on to the underlying thing the trait is on?

再问另一种方式,为什么Rust不能仅凭以下方式做正确的事情(tm):

Asking yet another way, why can't Rust just do The Right Thing(tm) with:

struct Bar<'a> {
    foo: &'a Foo,
}

Right Thing(tm)将被解释为等同于上面的声明?

where The Right Thing(tm) would be to interpret that as equivalent to the declaration above?

很抱歉提出问题,但是我觉得我在做一些非常基本的事情(使用特质作为通用方面),我不得不深入兔子的洞里,我想了解为什么兔子洞那么深.

Sorry to bombard with questions, but I felt like I was doing something pretty basic (use a trait as a generic facet), and I had to go down the rabbit hole pretty deep, and I'd like to understand why the rabbit hole is that deep.

错误消息:error: explicit lifetime bound required绝对无济于事,因为已经存在生命周期限制.

The error message: error: explicit lifetime bound required was decidedly unhelpful, because there is a lifetime bound already.

推荐答案

为什么生命期不属于特征是特征的对象

why the lifetime isn't for the object that the trait is a trait on

因为对特征对象的引用和特征对象本身可能具有不同的生存期.这是为参考而实现的特征的一个示例:

Because the reference to the trait object and the trait object itself might have different lifetimes. Here's an example of a trait that is implemented for a reference:

trait Quack {
    fn quack(&self) { println!("Quack") }
}

impl<'a> Quack for &'a bool {}

struct MasterQuack<'a> {
    q: &'a (Quack + 'a),
}

fn main() {
    let a = true;
    // a.quack(); // Nope, not defined
    (&a).quack();

    // MasterQuack {q: &a}; // Nope, this would be a reference to a boolean, which isn't Quack
    MasterQuack {q: &&a};
}

要注意的一件事是,拥有&'a (Trait + 'b)很好-也就是说,对本身具有/的特征的引用就是引用,并且这些生存期是不同的.你跟

One thing to note is that it's perfectly fine to have &'a (Trait + 'b) - that is, a reference to a trait that itself has a / is a reference, and those lifetimes are different. You said as much with

是否存在特征和基础对象具有不同生存期的情况?

Is there a case where the trait and the underlying object would have different lifetimes?

更多的是基础对象具有不同生命周期的引用".

But it's more a case of "the underlying object has references with different lifetimes".

为什么不能只做正确的事情(tm)

why can't Rust just do The Right Thing(tm)

截至 RFC 599 现在可以编译:

struct Bar<'a> {
    foo: &'a Foo,
}

这篇关于结构持有的特征对象中的显式生命周期声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆