ngo
ngo copied to clipboard
Use arc_new_cyclic
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?