函数指针的生命周期是for<'a,'_>而它应该是for<'r>
有时我会与生命作斗争。我还在学习,我不知道这里发生了什么:
use std::future::Future;
use futures::future::{BoxFuture, FutureExt};
struct M{}
struct Client{}
impl Client {
async fn send_and_expect<'a>(
&'a mut self,
m: &M
) -> std::result::Result<(), ()> {
Ok(())
}
pub fn connection_retrier<'a, T>(
f: fn(&'a mut Self, &M) -> T,
f_self: &'a mut Self,
f_m: &'a M,
)-> BoxFuture<'a, std::result::Result<(),()>>
where
T: Future<Output = std::result::Result<(), ()>> + 'a
{
async move {
Client::send_and_expect(f_self, f_m).await
}.boxed()
}
async fn send_with_retry<'a>(&'a mut self) -> std::result::Result<(), ()> {
let m = M{};
Client::connection_retrier(
Client::send_and_expect,
&mut *self, &m).await
}
}
错误:
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/lib.rs:31:21
|
11 | ) -> std::result::Result<(), ()> {
| --------------------------- the `Output` of this `async fn`'s found opaque type
...
31 | Client::send_and_expect,
| ^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'r> fn(&mut Client, &'r M) -> impl futures::Future`
found fn pointer `for<'a, '_> fn(&'a mut Client, &M) -> impl futures::Future
操场
我现在完全不明白为什么 in for<'r> fn(&mut Client, &'r M) -> impl futures::Future,&mut Client没有生命周期。又是什么_手段 为<'一'_> FN('一MUT客户,&M) - > IMPL期货:: Future`?
我非常有兴趣了解这里发生的事情。
回答
清理代码
为了从编译器的角度来看这段代码,让我们将本示例中的所有生命周期参数更改为显式并具有不同的名称,扩展async fn糖,然后查看错误如何变化。
上面的例子在生命周期推断和脱糖之后等价于下面的例子。
// name the second lifetime, and expand the async fn sugar.
fn send_and_expect<'a, 'b>(&'a mut self, m: &'b M) -> impl Future<Item=Result<(), ()>> + 'a + 'b
{ ... }
// rename 'a to 'c to avoid ambiguous lifetime names
pub fn connection_retrier<'c, T>(
f: for<'d> fn(&'c mut Self, &'d M) -> T, // name the implicit higher-ranked lifetime here
f_self: &'c mut Self,
f_m: &'c M,
)-> BoxFuture<'c, std::result::Result<(), ()>>
where T: Future<Output = std::result::Result<(), ()>> + 'c
{ ... }
// rename 'a to 'e to avoid ambiguous lifetime names
async fn send_with_retry<'e>(&'e mut self) -> std::result::Result<(), ()> {
进行此更改后,错误变为:
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/lib.rs:31:21
|
11 | ) -> std::result::Result<(), ()> {
| --------------------------- the `Output` of this `async fn`'s found opaque type
...
31 | Client::send_and_expect,
| ^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'d> fn(&mut Client, &'d M) -> impl futures::Future`
found fn pointer `for<'a, 'b> fn(&'a mut Client, &'b M) -> impl futures::Future`
这应该澄清你的问题'_:它只是编译器给推断的第二个生命周期参数的名称send_and_expect。至于 上缺少的生命周期&mut Client,你可以看到这里仍然缺少它。由于我不完全理解的原因,并且以取决于给出的确切错误消息的方式,编译器有时会在打印引用类型时省略具体的生命周期,但请不要误会,该引用的生命周期是 'c.
解决错误
进入实际问题。的签名f表示connection_retrier期待一个函数,它 (1) 获取生命周期的引用&'c和 (2)任何其他生命周期的引用,并且 (3) 返回一个Future类型,该类型将保持有效'c,如您的where绑定所指定的那样.
当我们传递send_and_expectto 时connection_retrier,为了使签名匹配,编译器将其强制转换为类型for<'d> send_and_expect::<'c, 'd>。但该类型不满足上述条件(3)!与常规函数不同,函数的默认行为async是在其返回类型中捕获所有输入生命周期,因此 的返回类型for<'d> send_and_expect::<'c, 'd>实际上是impl Future<Item=Result<(), ()>> + 'c + 'd,您可以通过查看send_and_expect的扩展签名来判断。
由于这种类型借用了两个生命周期'cand 'd,并且没有约束'd: 'c(阅读:'doutlives 'c)'c,如果'd首先结束,它可能不会在整个生命周期内保持有效。正是这种不匹配导致您收到相当神秘的生命周期错误。有两种方法可以解决此问题,具体取决于您喜欢的语义。您可以:
-
从
f整个中删除排名较高的界限,并准确指定您将在connection_retrier(两者&'c)中调用它的生命周期。pub fn connection_retrier<'c, T>( f: fn(&'c mut Self, &'c M) -> T f_self: &'c mut Self, f_m: &'c M, ) -> BoxFuture<'c, std::result::Result<(), ()>> where T: Future<Output = std::result::Result<(), ()>> + 'c { ... } -
保持
connection_retrier相同的签名并指定返回的未来send_and_expect仅从其第一个参数借用。要做到这一点,您需要将async fn糖滴在签名上并将身体包裹在一个async move块中。fn send_and_expect<'a, 'b>(&'a mut self, m: &'b M) -> impl Future<Output=Result<(), ()>> + 'a { async move { ... } }
请注意,您无法通过编写fas的类型来解决此问题for<'d: 'c> fn(&'c mut Self, &'d M) -> T,因为目前普遍量化的生命周期不允许有界限。