Crates.io | super_cell |
lib.rs | super_cell |
version | 0.1.1 |
source | src |
created_at | 2024-08-13 08:49:19.48724 |
updated_at | 2024-08-13 09:15:05.816245 |
description | A super (unsafe) cell that also implements send and sync regardless of the inner type's send/sync. |
homepage | |
repository | https://github.com/EasternGamer/super_cell |
max_upload_size | |
id | 1335709 |
size | 11,671 |
A super (unsafe) that modifies Cell
type to be more convenient and thus introduce potentially undefined behaviour for mutable types.
Undefined behaviour possible:
This leaves the safe-ness in the hands of the programmer, but generally in using this in my own projects, I have not come across a time when this has caused an issue.
use super_cell::*;
fn main() {
// Mutable primitive
let result = SuperCell::new(10);
*result.get_mut() = 11;
assert_eq!(*result.get(), 11); // OK
assert_eq!(*result.get_mut(), 11); // OK
// Mutable Complex Struct
let result = SuperCell::new(Test {
x: 0,
list: vec![],
});
let mutable = result.get_mut();
let mut list = vec![];
mutable.x = 100;
for i in 0..100 {
mutable.list.push(i);
list.push(i);
}
assert_eq!(result.get().x, 100); // OK
assert_eq!(result.get().list, list); // OK
assert_eq!(result.get_mut().x, 100); // OK
assert_eq!(result.get_mut().list, list); // OK
// Mutable Parallel/Async
let result = SuperCell::new(10);
thread::scope(|x| {
let reference = result.get_mut();
let handle = x.spawn(|| {
sleep(Duration::from_millis(10));
*reference = 11;
});
assert_eq!(*result.get(), 10); // OK
assert_eq!(*result.get_mut(), 10); // OK
handle.join().expect("Failed to join thread!");
assert_eq!(*result.get(), 11); // OK
assert_eq!(*result.get_mut(), 11); // OK
});
assert_eq!(*result.get(), 11); // OK
assert_eq!(*result.get_mut(), 11); // OK
}