Skip to content

Latest commit

 

History

History
26 lines (19 loc) · 506 Bytes

rust2.md

File metadata and controls

26 lines (19 loc) · 506 Bytes

Rust2 - sharing

Rust2 is an extension that adds access to shared data through Rc<T> and Arc<T>. This makes it possible for multiple structures to reference the same data simultaneously.

Examples

A duplicatable counter with usize

struct Counter(Rc<usize>);

impl Counter {
    fn inc(&mut self) {
        *Rc::get_mut(&mut self.0).unwrap() += 1;
    }

    fn val(&self) -> usize {
        *self.0
    }

    fn dup(&self) -> Counter {
        Counter(self.0.clone())
    }
}