Skip to content

Use arc_new_cyclic  #196

@tatetian

Description

@tatetian

The arc_new_cyclic feature has been stablized in Rust. 1.58. Since we are using recently new nightly version, I think we should already have this feature.

This enables creating self-referencing Arc object.

use std::sync::{Arc, Weak};

struct Foo {
    me: Weak<Foo>,
}

impl Foo {
    /// Construct a reference counted Foo.
    fn new() -> Arc<Self> {
        Arc::new_cyclic(|me| Foo {
            me: me.clone(),
        })
    }

    /// Return a reference counted pointer to Self.
    fn me(&self) -> Arc<Self> {
        self.me.upgrade()
    }
}

Since this pattern is so common, NGO even adds a dedicated crate, new-self-ref-arc for exactly this purpose. We should remove the crate and use new_cyclic instead.

Anyone interested in taking this refactoring task?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions