【问题标题】:Lifetime in recursive struct with mutable reference具有可变引用的递归结构中的生命周期
【发布时间】:2020-02-11 21:43:11
【问题描述】:

我正在尝试为树遍历定义一个类似于链表的递归结构。一个节点有一些数据并且可以访问它的父节点。子节点应该可变地借用它的父节点以确保独占访问,并在它被删除后释放它。我可以使用不可变引用来定义这个结构,但是当我使父引用可变时不能。在使父引用可变时,我被编译器错误弄糊涂了,不明白。

如何为这种具有可变父引用的递归结构定义生命周期?

这是一个最小的例子。这会编译但使用只读引用:

struct Node<'a> {
    // Parent reference. `None` indicates a root node.
    // I want this to be a mutable reference.
    pub parent: Option<&'a Node<'a>>,
    // This field just represents some data attached to this node.
    pub value: u32,
}

// Creates a root node
// I use a static lifetime since there's no parent for the root so there are no constraints there
fn root_node(value: u32) -> Node<'static> {
    Node {
        parent: None,
        value,
    }
}

// Creates a child node
// The lifetimes indicates that the parent must outlive its child
fn child_node<'inner, 'outer: 'inner>(
    parent: &'inner mut Node<'outer>,
    value: u32,
) -> Node<'inner> {
    Node {
        parent: Some(parent),
        value,
    }
}

// An example function using the struct
fn main() {
    let mut root = root_node(0);
    let mut c1 = child_node(&mut root, 1);
    let mut c2 = child_node(&mut c1, 2);
    {
        let mut c3 = child_node(&mut c2, 3);
        let c4 = child_node(&mut c3, 4);
        let mut cur = Some(&c4);
        while let Some(n) = cur {
            println!("{}", n.value);
            cur = n.parent;
        }
    }
    {
        let c5 = child_node(&mut c2, 5);
        let mut cur = Some(&c5);
        while let Some(n) = cur {
            println!("{}", n.value);
            cur = n.parent;
        }
    }
    println!("{}", c2.value);
}

Rust playground: immutable reference

我想要一个可变引用,所以我尝试替换 Node 结构以使用可变引用:

struct Node<'a> {
    // Parent reference. `None` indicates a root node.
    // I want this to be a mutable reference.
    pub parent: Option<&'a mut Node<'a>>,
    // This field just represents some data attached to this node.
    pub value: u32,
}

然后我收到以下错误:

error[E0623]: lifetime mismatch
  --> src/main.rs:25:22
   |
21 |     parent: &'inner mut Node<'outer>,
   |             ------------------------
   |             |
   |             these two types are declared with different lifetimes...
...
25 |         parent: Some(parent),
   |                      ^^^^^^ ...but data from `parent` flows into `parent` here

Rust playground: mutable reference

我不明白可变性与流入字段的数据之间的关系。在不可变的情况下,我已经要求函数传递可变/独占引用。我一直在尝试各种生命周期组合(使用单一生命周期、反转它们的关系等),但没有成功。

【问题讨论】:

  • 很确定你的整个概念是有缺陷的。这种结构将允许可变别名(head.next.next(head.next).next)。
  • @Shepmaster 我的目标是只有最低(活动)节点才能操作链中的数据。我不太了解您对可变别名的表示法。在我的示例中,一旦创建了c3,它就会借用c2(以及c1root),因此在删除c2 之前您无法访问它们,因此您不应该使用别名他们(至少这是目标)。
  • 这让我想起了How do I implement the Chain of Responsibility pattern using a chain of trait objects?,但我不确定它是否足够接近以标记重复。这个问题的答案(我的和 Lukas 的)能说明你的问题吗?
  • @trentcl 谢谢,我会调查的。为了提供更多上下文,我想使用这个结构在递归函数调用之间传递数据。每个节点代表一个函数调用级别。这确保首先创建父节点,我将其作为可变引用传递给下一次调用,它创建一个对该父节点具有独占访问权限的子节点,当内部调用结束时,子节点被删除,我重新获得对父节点。
  • @trentcl 您发布的问题确实相似。这实际上是一个使用特征的更普遍的问题,下一个节点是用方法设置的,而不是在构造时设置的。您对这个问题的回答实际上对应于我的不可变案例。问题是我需要可变性。尽管如此,我相信它帮助我解决了这个问题。我觉得这与终身差异有关。我想我记得结构中的不可变引用是协变的,但可变引用是不变的,但我不确定这是否意味着我的问题是可解决的。

标签: rust lifetime recursive-datastructures


【解决方案1】:

由于变异,不可能用可变引用实现这种递归结构。

The Rustonomicon has a section on variance,附下表:

|           | 'a        | T         |
|-----------|-----------|-----------|
| &'a T     | covariant | covariant |
| &'a mut T | covariant | invariant |

特别是,&amp;'a mut T 对于T 是不变的。

这里的核心问题是节点只知道其父节点的生命周期,而不知道其所有祖先的生命周期。即使在我的情况下,我只是对改变祖先的 value 字段感兴趣,&amp;mut Node 也可以修改链上任何祖先的 parent 字段,而我们无法访问精确的终生。

这是一个示例,其中我的结构可能会导致可变父引用不健全。 如果T&amp;'a mut T 中是协变的,则以下代码将被接受:

fn main() {
    let mut root: Node<'static> = root_node(0);

    // where 'a corresponds to `root`
    let mut c1: Node<'a> = child_node(&mut root, 1);
    {
        let mut evil_root: Node<'static> = root_node(666);
        {
            // where 'b corresponds to `c1`
            let mut c2: Node<'b>  = child_node(&mut c1, 2);
            // where 'c corresponds to `c2`
            let mut c3: Node<'c>  = child_node(&mut c2, 3);

            // Here is the issue: `c3` knows that its ancestors live at least as long
            // as `c2`. But it does not know how long exactly.
            // With covariance, the lifetime of `evil_root` would be compatible since
            // it outlives `c2`. And because `&mut T` enables to mutate any field
            // we could do the following:
            let c2_ref: &mut Node<'c> = c3.parent.unwrap();
            let c1_ref: &mut Node<'c> = c2_ref.parent.unwrap();
            *c1_ref.parent = Some(&mut evil_root);
        }
    }
    // Trying to access the parent of `c1` now causes a read-after-free
    println!("{}", c1.parent.unwrap().value);
}

不变性规则确保上面的代码被编译器拒绝并且没有不健全。

因为&amp;mut 允许修改任何字段,包括带有引用的字段,并且因为这种递归不会跟踪所有父生命周期,所以它是不合理的。 为了安全地实现这样的递归结构,Rust 需要一个允许变异 value 的引用(因为它具有静态生命周期,所以没有问题)但不是 parent。 在我上面发布的最小示例中,可以使用父级的不可变引用并将节点数据放在CellRefCell 后面来实现。另一种可能的解决方案(但我没有深入研究它)是将可变父引用放在Pin 后面,但取消引用它将是unsafe:我必须手动确保我永远不会更改@ 987654338@参考。

我的实际用例稍微复杂一些,因此我将尝试通过将数据存储在由Vec 支持的堆栈中来重组它以消除对递归结构的需求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多