#cell #arc #sync #data-structures

arc-cell

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

12 releases

0.3.3 Mar 23, 2022
0.3.1 Mar 15, 2022
0.2.2 Mar 12, 2022
0.1.5 Dec 12, 2016

#372 in Concurrency

Download history 3077/week @ 2024-07-20 1348/week @ 2024-07-27 992/week @ 2024-08-03 706/week @ 2024-08-10 670/week @ 2024-08-17 882/week @ 2024-08-24 590/week @ 2024-08-31 568/week @ 2024-09-07 620/week @ 2024-09-14 671/week @ 2024-09-21 664/week @ 2024-09-28 697/week @ 2024-10-05 726/week @ 2024-10-12 733/week @ 2024-10-19 740/week @ 2024-10-26 420/week @ 2024-11-02

2,728 downloads per month

MIT license

11KB
222 lines

arc-cell

Documentation

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

[dependencies]
arc-cell = "0.3"

Usage

Lightweight swappable Arc member

use std::sync::Arc;
use arc_cell::ArcCell;

pub struct Thing {
    data: ArcCell<Vec<u8>>,
}

impl Thing {
    pub fn update(&self, data: Arc<Vec<u8>>) {
        self.data.set(data);
    }
}

Self-referencial structure

use std::sync::Arc;
use arc_cell::WeakCell;

pub 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

Features