2 个版本
0.0.2 | 2022 年 10 月 6 日 |
---|---|
0.0.1 | 2022 年 10 月 6 日 |
#21 in #ref
17KB
174 行
引用
是一个简单的 Arc<Mutex<T>
系统,允许你始终读取值,但在修改数据时表现得像 Mutex!
⚠️仅在你知道自己在做什么时使用!
示例
-
1:
use ref_kman::Ref; pub struct State{ pub data: i32 } impl State{ pub fn new() -> Self{ Self{ data: 0 } } } fn main(){ let data = Ref::new(State::new()); // clone is acting like `Arc::clone()` // because RefInner is containt in a `Arc` // and you can share with others threads! let data_clone = data.clone(); // is not mutabile you need to get the motabile RefMut<T> // i create a scope because is needed to not block the thread. { let mut mut_data = data.get_mut(); // i can read the original data but no other thread can modifiy println!("Data: {}", data.data); // Data: 0 mut_data.data = 5; } println!("Data: {}", data_clone.data); // Data: 5 // and you can create a closure // this is better because you can call 2 times. // RefMut<T> // You only read and modify data // You cannot clone data.mut_scope(|mut_data|{ mut_data.data = 10; }); println!("Data: {}", data.data); }
依赖项
~0–10MB
~43K SLoC