Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use arc_new_cyclic #196

Open
tatetian opened this issue Jan 20, 2022 · 0 comments
Open

Use arc_new_cyclic #196

tatetian opened this issue Jan 20, 2022 · 0 comments

Comments

@tatetian
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant