gpt4 book ai didi

rust - 如何将引用生命周期绑定(bind)到函数局部范围

转载 作者:行者123 更新时间:2023-11-29 08:13:13 26 4
gpt4 key购买 nike

我想写一个函数 A,它以一个函数 B 作为参数,该函数以一个类型作为参数,该类型由一个引用类型参数化,该引用类型的生命周期至少是 A 主体中局部变量的生命周期。

考虑以下示例:

struct Foo {}

fn consume(mut v: Vec<&Foo>) {
while let Some(..) = v.pop() {
// Do stuff
continue;
}
}

fn setup_and<F>(f: F)
where
F: FnOnce(&mut Vec<&Foo>) + Send,
{
let mut v: Vec<&Foo> = vec![];
let other_foo = Foo {};

f(&mut v);
v.push(&other_foo);
consume(v);
}

fn main() {
let foo = Foo {};
setup_and(|v| {
v.push(&foo);
});
}

rustc 无法自行推断生命周期。它提示:

error[E0597]: `foo` does not live long enough
--> src/main.rs:25:17
|
24 | setup_and(|v| {
| --- value captured here
25 | v.push(&foo);
| --------^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `foo` is borrowed for `'static`
26 | });
27 | }
| - `foo` dropped here while still borrowed

我尝试为 setup_and 所采用的引用指定生命周期,如下所示:

fn setup_and<'a, F>(f: F)
where
F: FnOnce(&mut Vec<&'a Foo>) + Send,
{
let mut v: Vec<&'a Foo> = vec![];

现在 rustc 提示 setup_and 本地引用 other_foo 活得不够长。我认为这是因为它需要比 setup_and 范围更长的生命周期。

在这种情况下,我该如何正确绑定(bind)生命周期?我想表达的是,引用必须在 consume 调用结束之前有效。

最佳答案

您的实现中存在生命周期冲突的严重问题,如果不至少部分重新设计 struct 和方法的外部签名,就没有简单的解决方法。它们都源于 setup_and 方法,并在您明确描述生命周期边界时由编译器突出显示。

下面复制了您的方法主体,并附有注释以供您理解问题:

let mut v: Vec<&Foo> = vec![];
let other_foo = Foo {}; // other_foo is created here

f(&mut v);
v.push(&other_foo); // other_foo requires lifetime 'a to be added to this
consume(v); // consume does not restrict the lifetime requirement 'a
// other_foo is dropped here, at lifetime less than 'a

这个问题最简单的解决方案是存储一个 Arc<Foo> ,就像这样 ( playground ):

fn setup_and<F>(f: F)
where
F: FnOnce(&mut Vec<Arc<Foo>>) + Send,
{
let mut v: Vec<Arc<Foo>> = vec![];
let other_foo = Foo {};

f(&mut v);
v.push(Arc::new(other_foo));
consume(&mut v);
}

Arc 是一个原子引用计数指针。它是一个可克隆结构,用作指向堆上对象的动态指针;出于所有意图和目的,它只作为只读引用使用,不需要终身使用。当 Arc 的所有副本都被删除时,其中的项目也会被删除。

这解决了两个问题:

  1. 您的 other_foo 现在已Arc 中,不再导致其生命周期问题
  2. 您现在可以像访问引用一样访问您的对象(Arc 实现 Deref)

之所以选择 Arc,是因为您的 FnOnce 需要 Send,而 Rc(Arc 的单线程变体)无法提供。

关于rust - 如何将引用生命周期绑定(bind)到函数局部范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58184443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com