借用的价值对于“AsRef”结果来说存活时间不够长,即使它的“self”存活时间足够长

Borrowed value does not live long enough for `AsRef` result even if its `self` lives long enough

我有一个将一些功能包装在切片周围的结构:

use std::fmt::Debug;

struct SliceWrapper<'a, T: Debug + Copy + 'a> {
    slice: &'a [T],
    pos: usize,
}

我想为每个支持 AsRef<T: Debug + Copy + 'a> 的元素实现 From 特性,如下所示:

impl<'a, T: Debug + Copy + 'a, R: AsRef<[T]> + 'a> From<R> for SliceWrapper<'a, T> {
    fn from(slice: R) -> Self {
        Self {
            slice: slice.as_ref(),
            pos: 0,
        }
    }
}

我收到错误:

error[E0597]: `slice` does not live long enough
  --> src/lib.rs:11:20
   |
11 |             slice: slice.as_ref(),
   |                    ^^^^^ borrowed value does not live long enough
...
14 |     }
   |     - borrowed value only lives until here
   |
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 8:6...
  --> src/lib.rs:8:6
   |
8  | impl<'a, T: Debug + Copy + 'a, R: AsRef<[T]> + 'a> From<R> for SliceWrapper<'a, T> {
   |      ^^

我不明白,因为我说 R (slice) 必须和我的 SliceWrapper 一样长寿 – 据我了解,AsRef<_> 从它的 self (slice)...

继承生命周期

nightly 上的完整错误消息非常清楚地说明了这里发生的情况。您将 slice 移动到函数 from() 中,然后使用 as_ref() 借用它,然后它在作用域的末尾被删除:

8  | impl<'a, T: Debug + Copy + 'a, R: AsRef<[T]> + 'a> From<R> for SliceWrapper<'a, T> {
   |      -- lifetime `'a` defined here
9  |     fn from(slice: R) -> Self {
10 |         Self{ slice: slice.as_ref(), pos: 0 }
   |                      ^^^^^---------
   |                      |
   |                      borrowed value does not live long enough
   |                      argument requires that `slice` is borrowed for `'a`
11 |     }
   |     - `slice` dropped here while still borrowed

您正在尝试创建终身有效的借用 'a,但您借用的所有者 slice 的寿命不够长。

From 特征似乎不适合这个用例。该特征的想法是将某些数据的所有权转移到一种新类型,但您的 SliceWrapper 没有取得所有权。我建议编写一个自定义构造函数,而不是通过引用获取 R

您可以将转换限制为引用参数,然后您就有了一种很好的方式来表达您需要的借用——生命周期取自输入引用:

impl<'a, T, R> From<&'a R> for SliceWrapper<'a, T>
where
    T: Debug + Copy + 'a,
    R: AsRef<[T]>
{
    fn from(slice: &'a R) -> Self {
        Self {
            slice: slice.as_ref(),
            pos: 0,
        }
    }
}

现在您可以按照您原来的推理进行推理了。 你有 AsRef 给你: &'x self -> &'x [T] 对于任何 'x 并且你输入一个 &'a R 到它,所以你得到一个 &'a [T]

引用引用 From 有时存在,我认为它特别适用于类似引用到引用的转换。 std 中的一个示例是 impl<'a> From<&'a str> for Cow<'a, str>