Const 闭包数组采用对 Rust 中具有生命周期参数的结构的可变引用

Const array of closures taking a mutable reference to a struct with lifetime parameter in Rust

我在一个 Rust 项目中有以下场景:

struct Foo<'a> {
    stuff: &'a i32,
}

因此我告诉编译器我的 Foo 结构包含对某些东西的引用,我必须给它一个生命周期才能使它工作,很好。

现在,我宣布:

type FooFunc<'a> = &'a dyn Fn(&'a mut Foo<'a>) -> bool;

const funcs: [FooFunc; 4] = [
    &|f| { *f.stuff = 0; false },
    &|f| { *f.stuff = 1; true },
    &|f| { *f.stuff = 2; true },
    &|f| { *f.stuff = 3; true },
];

并尝试从 Foo:

的 "method" 中的 const 数组调用闭包
impl<'a> Foo<'a> {
    fn bar(&mut self, i: usize) -> bool {
        funcs[i](self)
    }
}

关于生命周期的推理,这应该没问题,因为 selfFoo 结构的引用具有生命周期 'a(肯定不会超过 'static ,那是闭包的生命周期)所以闭包应该能够毫无问题地接收 self 作为参数。

借用检查器有点同意这一点,但仍然报告一个我不明白的错误:

error[E0308]: mismatched types
 --> src/main.rs:7:18
  |
7 |         funcs[i](self)
  |                  ^^^^ lifetime mismatch
  |
  = note: expected type `&'static mut Foo<'static>`
             found type `&'static mut Foo<'a>`
note: the lifetime 'a as defined on the impl at 5:6...
 --> src/main.rs:5:6
  |
5 | impl<'a> Foo<'a> {
  |      ^^
  = note: ...does not necessarily outlive the static lifetime

error[E0312]: lifetime of reference outlives lifetime of borrowed content...
 --> src/main.rs:7:18
  |
7 |         funcs[i](self)
  |                  ^^^^
  |
  = note: ...the reference is valid for the static lifetime...
note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the method body at 6:5
 --> src/main.rs:6:5
  |
6 | /     fn bar(&mut self, i: usize) -> bool {
7 | |         funcs[i](self)
8 | |     }
  | |_____^

它告诉我(在 note 标签中)the lifetime 'a as defined on the impl at 5:6 does not necessarily outlive the static lifetimethe reference is valid for the static lifetime but the borrowed content is only valid for the anonymous lifetime #1 defined on the method body at 6:5。虽然给出此类错误消息的编译器很棒,但我仍然不明白问题出在哪里。

谈到第二个 note(这是(试图)解释问题的那个),我不明白借用的内容是 const 数组还是 self参考,但无论哪种情况:

当然,我在这里遗漏了一些东西,我们将不胜感激。

注意:post中的所有代码只是我正在尝试实现的"architecture"的精简版本——当然您在示例代码中看到的逻辑可以以一种简单的方式轻松实现,但这不是我需要的。我想对我的结构进行 table 操作(因此想法使元素闭包接受 &mut 引用)和 运行 这些操作来自 "method" 我的结构。实际项目中的实际类型是 [Option<[FooFunc<'a>; 6]>; 256],所以我们谈论的是一个相当大的二维 "table",我想用 match 语句实现它会变得非常不愉快,尤其是考虑到我经常重复使用 FooFunc

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=17034fe41c74d65d7a296ae812f19945

type FooFunc<'a> = &'a dyn Fn(&'a mut Foo<'a>) -> bool;不是 "A FooFunc is a reference of any lifetime to a closure which takes a reference of any lifetime (to a Foo of the same lifetime)"。相反,它说 "A FooFunc<'a> is a reference with a specific lifetime 'a to a closure which takes a reference of that specific lifetime 'a (to a Foo of the same specific lifetime 'a), which is the same every time that closure is called."

此外,当您在项目声明 ([FooFunc; 4]) 中省略生命周期时,它被推断为 'static。 (这是因为类型本身不能超过它们的类型参数,但项目必须是 'static,所以唯一有效的生命周期参数也是 'static。)所以 funcs 是一个数组引用只接受 &'static mut Foo<'static>.

的闭包

您可能想要的是 type FooFunc<'r> = &'r for<'a, 'b> dyn Fn(&'a mut Foo<'b>) -> bool;,但由于其他地方的可变性问题,这仍然失败。也就是说 "A FooFunc<'r> is a reference of lifetime 'r to a closure which accepts references of any lifetime to Foos with any lifetime (the latter lifetime implicitly outlives the first lifetime)." 'r 仍将被推断为 'static 但这没关系,因为这就是你所拥有的。