簡體   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