【问题标题】:Lifetime bound in Async function which is also an argumentAsync 函数中的生命周期限制,这也是一个参数
【发布时间】:2021-12-28 06:40:18
【问题描述】:

我正在尝试将异步函数作为参数传递。异步函数接受一个引用,因为它是争论的。

use std::future::Future;

async fn f(x: &i32) -> i32 {
    todo!()
}

async fn g<F, Fut>(f: F)
where
    F: Send + Sync + 'static + for<'a> Fn(&'a i32) -> Fut,
    Fut: Future<Output = i32> + Send + Sync,
{
//    let x = 3;
//    f(&x).await;
}

#[tokio::main]
async fn main() {
    g(f).await;
}

但是我有编译错误。

error[E0308]: mismatched types
  --> src/main.rs:18:5
   |
18 |     g(f).await;
   |     ^ lifetime mismatch
   |
   = note: expected associated type `<for<'_> fn(&i32) -> impl Future<Output = i32> {f} as FnOnce<(&i32,)>>::Output`
              found associated type `<for<'_> fn(&i32) -> impl Future<Output = i32> {f} as FnOnce<(&'a i32,)>>::Output`
   = note: the required lifetime does not necessarily outlive the empty lifetime
note: the lifetime requirement is introduced here
  --> src/main.rs:9:55
   |
9  |     F: Send + Sync + 'static + for<'a> Fn(&'a i32) -> Fut,
   |                                                       ^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `test-async-tokio` due to previous error

这里Fut为什么要引入lifetime? 如何指定这段代码的生命周期?

最好的问候!

【问题讨论】:

  • 如果您在 trait bound 中更改为像 g&lt;'a, ...&gt; 这样的适当生命周期参数而不是 for&lt;'a&gt;,则错误会稍微清晰一些:x 借用生命周期 'a 但在末尾删除范围。仍然试图弄清楚为什么这是一个问题,因为你等待它......
  • 如你所说,我已更改为终身注释。但是g 使用了一个局部变量x。并且对x 的引用不能限制为'a。 F: Send + Sync + 'static + Fn(&amp;'a i32) -&gt; Fut,
  • This Comment 效果很好。我可以将异步函数移动到结构包装器。但这有点复杂。

标签: asynchronous rust arguments lifetime


【解决方案1】:

从进一步的测试来看,我最初的建议似乎是将特征上的 for&lt;'a&gt; 子句中的 'a 生命周期参数更改为绑定到适当的泛型参数,这导致编译器认为生命周期存在于返回的未来中,这可以防止使用本地人。即使我明确地将'a 生命周期绑定到Fut 并等待结果,情况似乎也是如此。

我不完全确定您建立的特征界限为什么不起作用,但我相信这是由于异步函数返回 impl Future 而不是已知的具体类型。我在与您的原始代码有一些偏差时遇到了这个问题。 This source 似乎可以解决您的问题,我在下面为您的特定用例提供了一个修改示例。 注意:我将f 参数重命名为y,以强调它不是直接调用f 函数。

此解决方案添加了一个新特征(带有全面 impl),可以直接用作F 的特征绑定,如果输入/输出是引用,则需要 for&lt;'a&gt; 子句。我可能弄错了,但这似乎可行,因为未知的具体未来类型作为关联类型被包装到新特征中,因此 g 及其特征边界不需要直接担心。

use std::future::Future;

trait AsyncFn<T>: Fn(T) -> <Self as AsyncFn<T>>::Fut {
    type Fut: Future<Output = <Self as AsyncFn<T>>::Output>;
    type Output;
}
impl<T, F, Fut> AsyncFn<T> for F where F: Fn(T) -> Fut, Fut: Future {
    type Fut = Fut;
    type Output = Fut::Output;
}

async fn f(_: &i32) -> i32 {
    todo!()
}

async fn g<F>(y: F) where F: for<'a> AsyncFn<&'a i32, Output = i32> {
    let x = 3;
    let res = y(&x).await;
}

#[tokio::main]
async fn main() {
    g(f).await;
}

【讨论】:

  • 真诚感谢您的回答。这个助手AsyncFn 太棒了。正如here 所提到的,您的解释似乎是正确的,但我仍然对生命周期感到困惑。我还有很多东西要学。
  • @readytogo 老实说,我仍然对错误的确切原因感到困惑,但这很好地解决了它。生命周期通常并没有那么糟糕,但是 async 让它变得一团糟,因为它们实际上是在后台返回状态机,而本地人是该状态的一部分。
  • 我是从别人那里学到的。正如rust rfc document 中提到的,async fn f(x: &amp;i32) -&gt; i32 {...} 具有与fn f&lt;'a&gt;(arg: &amp;'a str) -&gt; impl Future&lt;Output = i32&gt; + 'a { ... } 等效的类型签名。而gF: Send + Sync + 'static + for&lt;'a&gt; Fn(&amp;'a i32) -&gt; Fut,中的函数签名。 Fut 不受生命周期 'a 的限制。所以Ff是不一样的。
猜你喜欢
  • 2016-05-04
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2023-02-24
  • 2021-06-28
  • 1970-01-01
相关资源
最近更新 更多