[英]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.