Crates.io | rcrefcell |
lib.rs | rcrefcell |
version | 1.0.7 |
source | src |
created_at | 2022-05-31 02:20:30.051644 |
updated_at | 2022-07-16 02:33:09.219848 |
description | Wrapper type for Rc |
homepage | |
repository | |
max_upload_size | |
id | 597217 |
size | 5,322 |
Simple wrapper for Rc<RefCell<A>>
#[derive(Debug, PartialEq)]
struct Data<A> {
value: A
}
impl<A> Data<A> {
fn new(value: A) -> Data<A> {
Data {
value,
}
}
}
let data: Data<i32> = Data::new(1);
let counter: RcCell<Data<i32>> = RcCell::new(data);
let counter_a: RcCell<Data<i32>> = counter.clone();
let counter_b: RcCell<Data<i32>> = counter.clone();
counter_a.update(|mut v| v.value += 1);
counter_b.borrow_mut().value *= 3;
assert_eq!(counter.borrow().value, 6);
assert_eq!(*counter_a.borrow(), *counter_b.borrow());