Crates.io | cbsk_mut_data |
lib.rs | cbsk_mut_data |
version | 2.0.2 |
source | src |
created_at | 2023-12-09 09:20:28.155949 |
updated_at | 2024-09-04 09:11:52.241419 |
description | cbsk_mut_data is a ref mut tool |
homepage | |
repository | https://github.com/lifeRobot/cbsk/tree/master/libs/cbsk_mut_data |
max_upload_size | |
id | 1063580 |
size | 17,757 |
cbsk_mut_data is a ref mut tool
you can use cbsk_mut_data for static data modification, etc
the idea for this cargo crate comes
from dark-std(github)
this cargo crate may be unsafe, if you want safe data, you can
use dark-std(github)
Cargo.toml file :
cbsk_mut_data = "2.0.2"
main.rs file :
use cbsk_mut_data::mut_data_obj::MutDataObj;
fn main() {
let b = MutDataObj::new(true);
println!("b is {b}");// b is true
b.set(false);
println!("b is {b}");// b is false
b.trigger();
println!("b is {b}");// b is true
}
main.rs file (use struct) :
use cbsk_mut_data::mut_data_obj::MutDataObj;
#[derive(Default, Debug)]
struct A {
data: MutDataObj<i32>,
}
fn main() {
let a = A::default();
println!("a is {a:?}");// a is A { data: 0 }
a.data.set(10);
println!("a is {a:?}");// a is A { data: 10 }
}
main.rs file (in struct)
use cbsk_mut_data::mut_data_obj::MutDataObj;
#[derive(Default, Debug)]
struct A {
b: MutDataObj<B>,
}
#[derive(Default, Debug)]
struct B {
data: i32,
}
fn main() {
let a = A::default();
println!("a is {a:?}");// a is A { b: B { data: 0 } }
a.b.as_mut().data = 10;
println!("a is {a:?}");// a is A { b: B { data: 10 } }
}
Cargo.toml file :
once_cell = "1.19.0"
cbsk_mut_data = "2.0.2"
main.rs file :
use cbsk_mut_data::mut_data_obj::MutDataObj;
use once_cell::sync::Lazy;
pub static B: Lazy<MutDataObj<bool>> = Lazy::new(MutDataObj::default);
fn main() {
println!("b is {}", B.as_ref());// b is false
B.set(true);
println!("b is {}", B.as_ref());// b is true
B.trigger();
println!("b is {}", B.as_ref());// b is false
}
Cargo.toml file :
cbsk_mut_data = "2.0.2"
main.rs file :
use std::sync::Arc;
use cbsk_mut_data::mut_data_obj::MutDataObj;
use cbsk_mut_data::mut_data_ref::MutDataRef;
fn main() {
// unsafe example
let mut a = MutDataRef::new(&mut 0);
println!("a is {a:?}");// a is 0
let b = a.clone();
*a = 10;
println!("b is {b:?}");// b is 10
// mut arc
// mut arc may have many scenarios and can be very useful, but it is not recommended for you to use it this way
let c = Arc::new(MutDataObj::new(0));
println!("c is {c:?}");// c is 0
let d = c.clone();
c.set(10);
println!("d is {d:?}");// d is 10
}