为闭包实现特征会导致绑定/具体的生命周期不匹配

Luk*_*odt 8 traits lifetime rust

我想为特定类型的闭包实现一个特征.这是一个最小的例子(游乐场):

trait Foo {
    fn foo(&self, x: &u32);
}

impl<F> Foo for F
    where F: Fn(&u32)
{
    fn foo(&self, x: &u32) {
        self(x)
    }
}

fn main() {
    let _: &FnOnce(&u32) = &|x| {};   // works
    let _: &Foo          = &|x| {};   // doesn't work
}
Run Code Online (Sandbox Code Playgroud)

它导致此错误:

error: type mismatch resolving `for<'r> <[closure@<anon>:16:29: 16:35] as std::ops::FnOnce<(&'r u32,)>>::Output == ()`:
 expected bound lifetime parameter ,
    found concrete lifetime [--explain E0271]
  --> <anon>:16:28
   |>
16 |>     let _: &Foo          = &|x| {};
   |>                            ^^^^^^^
note: required because of the requirements on the impl of `Foo` for `[closure@<anon>:16:29: 16:35]`
note: required for the cast to the object type `Foo`

error: type mismatch: the type `[closure@<anon>:16:29: 16:35]` implements the trait `std::ops::Fn<(_,)>`, but the trait `for<'r> std::ops::Fn<(&'r u32,)>` is required (expected concrete lifetime, found bound lifetime parameter ) [--explain E0281]
  --> <anon>:16:28
   |>
16 |>     let _: &Foo          = &|x| {};
   |>                            ^^^^^^^
note: required because of the requirements on the impl of `Foo` for `[closure@<anon>:16:29: 16:35]`
note: required for the cast to the object type `Foo`
Run Code Online (Sandbox Code Playgroud)

我已经尝试将HRTB显式添加到这样的where子句中:

where F: for<'a> Fn(&'a u32)
Run Code Online (Sandbox Code Playgroud)

但它没有帮助.我也在impl块上声明了生命周期,如下所示:

impl<'a, F> Foo for F
    where F: Fn(&'a u32) { ... }
Run Code Online (Sandbox Code Playgroud)

但这会导致impl块内的生命周期错误.我认为这些错误是正确的,并且不能在impl块上声明生命周期参数.

我该如何修复这个例子?

She*_*ter 5

看看这部分错误:

[...]实现特性std::ops::Fn<(_,)>,但for<'r> std::ops::Fn<(&'r u32,)>需要特征

我认为基本上没有足够的代码来允许正确推断类型.添加显式类型注释允许编译示例:

let _: &Foo          = &|x: &u32| {};
Run Code Online (Sandbox Code Playgroud)