#sync #arc #cell

arc-cell

Helper for a simple Cell-like object containing Arc/Weak

6 releases

Uses old Rust 2015

0.1.5 Dec 12, 2016
0.1.4 Dec 11, 2016

#26 in #cell

MIT license

5KB
118 lines

arc-cell

Documentation

A simple library for a concurrent Cell-like object containing an Arc/Weak reference.

[dependencies]
arc-cell = "0.1"

usage

self-referencial structure

use arc_cell::WeakCell;

struct Thing {
    self_ref: WeakCell<Thing>,
    // ...
}

impl Thing {
    pub fn new() -> Arc<Thing> {
        let thing = Arc::new(Thing {
            self_ref: WeakCell::empty(),
        });
        
        thing.self_ref.store(&thing);
        thing
    }
    
    pub fn clone_ref(&self) -> Arc<Thing> {
        self.self_ref.upgrade().expect("This should be valid if we have a valid self")
    }
}

No runtime deps