繁体   English   中英

特征关联类型生命周期和自我

[英]Trait associated type lifetime and self

我有一个包装std::cell::Ref并通过引用基础值提供访问的结构。 像这样的东西:

use std::cell::Ref;

struct IntAccess<'a> {
    i: Ref<'a, i32>,
}

impl IntAccess<'_> {
    fn get(&self) -> &i32 {
        &self.i
    }
}

这工作正常。 由于我有多个这样的结构,我想定义一个共同的特征:

trait Access {
    type Item;
    fn get(&self) -> Self::Item;
}

但是,在尝试为IntAccess实施Access时遇到了麻烦:

impl<'a> Access for IntAccess<'a> {
    type Item = &'a i32;

    fn get(&self) -> Self::Item {
        &self.i
    }
}

它失败并出现以下错误:

error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
  --> src/main.rs:23:9
   |
23 |         &self.i
   |         ^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime defined here...
  --> src/main.rs:22:12
   |
22 |     fn get(&self) -> Self::Item {
   |            ^^^^^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:23:9
   |
23 |         &self.i
   |         ^^^^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined here...
  --> src/main.rs:19:6
   |
19 | impl<'a> Access for IntAccess<'a> {
   |      ^^
note: ...so that the types are compatible
  --> src/main.rs:22:33
   |
22 |       fn get(&self) -> Self::Item {
   |  _________________________________^
23 | |         &self.i
24 | |     }
   | |_____^
   = note: expected `<IntAccess<'a> as Access>`
              found `<IntAccess<'_> as Access>`

我想我有点明白编译器试图告诉我的内容:我试图借用self.i ,它的匿名生命周期与Self::Item ( 'a ) 的生命周期无关。 因此,似乎需要以某种方式将self的生命周期与get生命周期'a联系起来。 有没有办法做到这一点?

我不得不承认我没有完全理解这个问题。 由于我将生命周期'a传递给Ref ,并且Ref在内部存储了此生命周期的引用,因此在我看来,应该有可能以某种方式从中获取生命周期'a的引用,而不必明确地将self绑定到这个一生也是如此。 那么我在这里错过了什么?

你的理解是正确的。 关于您的疑问,让我们命名生命周期以便于调试:

impl<'a> Access for IntAccess<'a> {
    type Item = &'a i32;

    fn get<'b>(&'b self) -> Self::Item {
        &self.i
    }
}

self的类型是&'b IntAccess<'a> 'b短于或等于'a - 这是self格式良好(即能够存在)所必需的(否则,它将包含一个悬空引用)。

出于这个原因,我们不能借用self超过'b - 或者我们可以做类似的事情:

let v: IntAccess<'a>;
let inner: &'a i32 = {
    let r: &'b IntAccess<'a> = &v;
    <IntAccess<'a> as Access>::get(r)
}
let mut_r: &mut IntAccess<'a> = &mut v;

我们对inner (部分)有一个共享的和可变的引用!

如果没有Generic Associated Types ,您的问题将无法完全解决。 它们允许您通过生命周期参数化关联类型,使其能够表达“我想返回与self生命周期相关的关联类型”:

#![feature(generic_associated_types)]

trait Access {
    type Item<'a>
    where
        Self: 'a;
    fn get<'a>(&'a self) -> Self::Item<'a>;
    // Or, with lifetime elision:
    // fn get(&self) -> Self::Item<'_>;
}

impl<'a> Access for IntAccess<'a> {
    type Item<'b> = &'b i32
    where
        'a: 'b;

    fn get<'b>(&'b self) -> Self::Item<'b> {
        &self.i
    }
}

游乐场

我们可以在稳定版上做到这一点吗? 我们可以效仿。 我们不会为IntAccess本身实现Access ,而是实现它以引用它

trait Access {
    type Item;
    fn get(self) -> Self::Item;
}

impl<'a, 'b> Access for &'b IntAccess<'a> {
    type Item = &'b i32;
    fn get(self) -> &'b i32 {
        &self.i
    }
}

操场

这并不总是有效,因此不能完全替代 GAT,但在这种情况下已经足够好了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM