请考虑以下代码,其中包装了对根类型R
的引用。还存储了某种类型的N
(avigate),它知道如何为R
解除对T
的引用。
use std::ops::Deref;
struct Wrapper<'r, R, N, T>
where
N: Fn(&'r R) -> &T,
T: 'static,
{
r: &'r R,
n: N,
}
impl<'r, R, N, T> Deref for Wrapper<'r, R, N, T>
where
N: Fn(&'r R) -> &T,
T: 'static,
{
type Target = T;
fn deref(&self) -> &T {
let r: &'r R = self.r;
let t: &'r T = (self.n)(r);
t
}
}
现在,如果我们将引用类型r: &'r R,
更改为可变的r: &'r mut R,
,它将不再起作用:
use std::ops::Deref;
struct Wrapper<'r, R, N, T>
where
N: Fn(&'r R) -> &T,
T: 'static,
{
r: &'r mut R,
n: N,
}
impl<'r, R, N, T> Deref for Wrapper<'r, R, N, T>
where
N: Fn(&'r R) -> &T,
T: 'static,
{
type Target = T;
fn deref(&self) -> &T {
let r: &'r R = self.r;
let t: &'r T = (self.n)(r);
t
}
}
错误:
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/lib.rs:21:24
|
21 | let r: &'r R = self.r;
| ^^^^^^
|
note: ...the reference is valid for the lifetime 'r as defined on the impl at 13:6...
--> src/lib.rs:13:6
|
13 | impl<'r, R, N, T> Deref for Wrapper<'r, R, N, T>
| ^^
note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the method body at 20:5
--> src/lib.rs:20:5
|
20 | / fn deref(&self) -> &T {
21 | | let r: &'r R = self.r;
22 | | let t: &'r T = (self.n)(r);
23 | | t
24 | | }
| |_____^
我们通过nll得到更好的错误消息:
error: lifetime may not live long enough
--> src/lib.rs:21:16
|
13 | impl<'r, R, N, T> Deref for Wrapper<'r, R, N, T>
| -- lifetime `'r` defined here
...
20 | fn deref(&self) -> &T {
| - let's call the lifetime of this reference `'1`
21 | let r: &'r R = self.r;
| ^^^^^ type annotation requires that `'1` must outlive `'r
我在deref中注释了生命周期,以确保我与编译器在生命周期上处于同一轨道。 nll消息特别有趣,因为它说需要&self
才能使'r
失效。
但这对我来说没有意义,因为如果我们在deref上标注生命周期,它应该看起来像这样:
fn deref<'1>(&'1 self) -> &'1 T;
而是要求使用'r: '1
隐含的Wrapper<'r, ...>
这个直觉似乎在第一个示例中成立,但在第二个示例中使用不变的引用却没有。
所以我遇到了两个问题:
self.r
是不可变的,为什么会有所不同?由于r
是不可变的,因此无论如何我都无法可变地访问&self
。1.
是一项基本限制,还是可以通过注释方式告诉rustc我要做什么?答案 0 :(得分:0)
特质类型在其通用参数上是不变的。
考虑以下示例:
struct Test<'a, F: Fn(&'a i32)> {
i: &'a i32,
f: F,
}
fn main() {
let i = 1i32;
let t = Test { i: &i, f: |&_| {} };
{
let j = 2i32;
(t.f)(&j);
}
println!("{:?}", t.i);
}
这将给出错误:
error[E0597]: `j` does not live long enough
--> src/main.rs:12:15
|
12 | (t.f)(&j);
| ^^ borrowed value does not live long enough
13 | }
| - `j` dropped here while still borrowed
14 |
15 | println!("{:?}", t.i);
| --- borrow later used here
如您所见,类型Test<'a ...
并没有统一为寿命较短的j
,因为Test
包含特征隐含类型N
(静态调度) 。结果,它将'a
不变,因此'a
不能被缩短。但是j
对于'a
不适用,因此会出现错误。
移动到您的问题,让我们看一下代码的最低版本:
struct Wrapper<'r, R, N>
where
N: Fn(&'r R),
{
r: &'r mut R,
n: N,
}
impl<'r, R, N> Wrapper<'r, R, N>
where
N: Fn(&'r R),
{
fn myderef(&self) {
(self.n)(self.r)
}
}
这将产生相同的错误:
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/lib.rs:14:18
|
14 | (self.n)(self.r)
| ^^^^^^
|
note: ...the reference is valid for the lifetime 'r as defined on the impl at 9:6...
--> src/lib.rs:9:6
|
9 | impl<'r, R, N> Wrapper<'r, R, N>
| ^^
note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the method body at 13:5
--> src/lib.rs:13:5
|
13 | / fn myderef(&self) {
14 | | (self.n)(self.r)
15 | | }
| |_____^
这里到底发生了什么?有效期为&self
的类型为&'shorter_lifetime Wrapper<'r, R, N>
,而不是&'shorter_lifetime Wrapper<'shorter_lifetime, R, N>
。 'r
不会缩短为'shorter_lifetime
,因为Wrapper
会因'r
而在其通用生命周期参数N
上保持不变。
现在我们知道参数类型&self
的确切含义,让我们看看myderef()
主体内部发生了什么。特征类型N
(静态调度)是用self.r
调用的。但是self.r
是可变的引用,传递给(self.r)()
时会重新借用。因此,现在您有了一个可变的引用,该引用位于另一个引用(self
是引用)的后面,并且需要为'r
(N
需要根据定义将其输入参数的生存期设为'r
,因此&self
也需要生存'r
。但是&self
的生存期为'shorter_lifetime
,因此会出现错误。
换句话说,如果您将&'a & 'b mut T
('a
和'b
之间没有子类型关系)作为函数的输入参数,并且编译器允许您重新借用内部引用并返回它,则违反了借用规则,因为&mut T
已经在引用后面。外部引用“拥有”内部引用,主要是因为内部引用是可变的,并且函数需要保证只要重新借用内部(可变)引用,外部引用就至少保留,否则,在函数调用之后将是可变引用的一个以上所有者。
作为示例,以下代码将无法编译:
fn test<'a, 'b> (i:&'a &'b mut i32) -> &'b i32 {
&**i
}
但是这个会:
fn test<'a:'b, 'b> (i:&'a &'b mut i32) -> &'b i32 {
&**i
}
因为可以保证'a
的寿命至少要长到'b
。
如果内部引用是不可变的,则前者也将编译,因为您可以具有多个不可变的引用。没有外部引用“拥有”内部引用的概念。
要编译最低版本,我们必须告诉编译器&self
也适用于'r
。要么删除'r
的输入参数上的N
的硬约束(生存期省略)。
在您的示例中,deref()
不允许您根据&self
的定义在Deref
上指定生存期。如果您取消了'r
的输入参数上的N
的硬约束,则it will compile