实现闭包特征会导致 bound/concrete 生命周期不匹配

Implementing a trait for closures results in bound/concrete lifetime mismatch

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

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
}

导致此错误:

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`

我已经尝试将 HRTB 显式添加到 where 子句中,如下所示:

where F: for<'a> Fn(&'a u32)

但这并没有帮助。我还在 impl 块上声明了生命周期,如下所示:

impl<'a, F> Foo for F
    where F: Fn(&'a u32) { ... }

但这会导致 impl 块中出现生命周期错误。我 认为 这些错误是正确的,生命周期参数不能在 impl 块上声明。

我该如何修正这个例子?

免责声明:我不知道自己在做什么。

以下作品:

trait Foo<'a> {
    fn foo(&self, x: &'a u32);
}

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

查看这部分错误:

[...] implements the trait std::ops::Fn<(_,)>, but the trait for<'r> std::ops::Fn<(&'r u32,)> is required

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

let _: &Foo          = &|x: &u32| {};

这是部分答案,从一个有趣的实验开始:

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 f1: &Fn(&u32) = &|_x| {}; 
    let f2: &Foo = &f1;
    // but this fails:
    // let f3: &Foo = &|_x| {};
    f2.foo(&3);
}

(Playground)

我所做的就是将 FnOnce 更改为 Fn 以与特征保持一致,并将您的第一个闭包分配给类型 &Foo 的绑定 - 这一个有效.

这告诉我 trait 本身没问题——在创建 trait 对象时推断闭包的类型是个问题。回到错误,我们被告知闭包实现了 std::ops::Fn<(_,)>,但是 for<'r> std::ops::Fn<(&'r u32,)> 是必需的。这意味着您尝试的第一件事(将 for<'r>... 添加到特征中)没有任何效果,因为特征 already 需要这个。

此时我陷入困境 - 我不认为我足够详细地理解闭包的推理规则以了解为什么存在差异或如何使其起作用。我希望有人会来填写!