迫使结构超过另一个结构

发布于 2025-02-03 21:04:37 字数 1333 浏览 1 评论 0原文

我有两个结构。第一个“父”结构是需要明确清理的资源,我通过得出drop特征来处理的资源。第二个“儿童”结构是从父结构中检索出来的,并且所述资源尚未被交易为有效。

换句话说,任何父母的实例都必须超过其子女。

我的解决方案使用参考来使借用检查器执行此规则。

struct Parent {
    // Private resource data.
}

impl Parent {
    fn new() -> Self {
        Parent {}
    }

    fn new_child(&self) -> Child {
        return Child { _parent: self };
    }
}

impl Drop for Parent {
    fn drop(&mut self) {
        // Do some cleanup after which no instance of Child will be valid.
    }
}

struct Child<'a> {
    _parent: &'a Parent,
    // Private data.
}

impl<'a> Child<'a> {
    fn hello(&self) {
        println!("Hello Child!");
    }
}

fn main() {
    let parent = Parent::new();
    let child = parent.new_child();
    child.hello();

    // Fails to compile with the following line uncommented (as intented).
    // drop(parent);
    // child.hello();
}

这有效,但是child&nbsp;实际上并不需要了解其父母。我想将参考字段替换为phantomdata

struct Child<'a> {
    _parent: PhantomData<&'a Parent>,
    // Private data.
}

但是,在这种情况下,我如何“绑定” _Parent&nbsp; in parent in parent: :new_child

I have two structs. The first, "parent" struct holds a resource that needs explicit cleanup, which I am handling by deriving the Drop trait. The second, "child" struct is retrieved from the parent struct and needs that said resource hasn't been deallocated to be valid.

In other words, any parent instance must outlive its children.

My solution uses references to make the borrow checker enforce this rule.

struct Parent {
    // Private resource data.
}

impl Parent {
    fn new() -> Self {
        Parent {}
    }

    fn new_child(&self) -> Child {
        return Child { _parent: self };
    }
}

impl Drop for Parent {
    fn drop(&mut self) {
        // Do some cleanup after which no instance of Child will be valid.
    }
}

struct Child<'a> {
    _parent: &'a Parent,
    // Private data.
}

impl<'a> Child<'a> {
    fn hello(&self) {
        println!("Hello Child!");
    }
}

fn main() {
    let parent = Parent::new();
    let child = parent.new_child();
    child.hello();

    // Fails to compile with the following line uncommented (as intented).
    // drop(parent);
    // child.hello();
}

This works, but the Child doesn't actually need to know its parent. I thought to replace the reference field with a PhantomData:

struct Child<'a> {
    _parent: PhantomData<&'a Parent>,
    // Private data.
}

But in this case, how can I "bind" _parent to the parent instance, in Parent::new_child?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2025-02-10 21:04:37

您可以做以下操作:

struct Parent {
    // Private resource data.
}

struct Child<'a> {
    _parent: PhantomData<&'a Parent>,
    // Private data.
}

impl Parent {
    // The returned child borrows from self
    fn new_child(&self) -> Child<'_> {
        Child { _parent: PhantomData }
    }
}

这应该给您带来所需的行为。

You could do the following:

struct Parent {
    // Private resource data.
}

struct Child<'a> {
    _parent: PhantomData<&'a Parent>,
    // Private data.
}

impl Parent {
    // The returned child borrows from self
    fn new_child(&self) -> Child<'_> {
        Child { _parent: PhantomData }
    }
}

This should give you the desired behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文