Crates.io | ref-swap |
lib.rs | ref-swap |
version | 0.1.2 |
source | src |
created_at | 2023-05-30 15:46:30.365181 |
updated_at | 2023-06-06 14:20:51.218577 |
description | Safe wrapper around AtomicPtr |
homepage | |
repository | https://github.com/nitrokey/ref-swap |
max_upload_size | |
id | 878026 |
size | 36,270 |
Safe wrapper around AtomicPtr
.
Instead of swapping a pointer, it works with references and lifetimes, allowing a safe API.
Two versions are provided:
RefSwap
for swapping referencesOptionRefSwap
for swapping Option<&T>
.
OptionRefSwap
encodes None
as a null pointer and has no additionnal overhead.use ref_swap::RefSwap;
use core::sync::atomic::Ordering;
let a = 10;
let b = 20;
let reference = RefSwap::new(&a);
// In another thread
let loaded = reference.load(Ordering::Relaxed);
assert_eq!(loaded, &a);
assert!(core::ptr::eq(loaded, &a));
reference.store(&b, Ordering::Relaxed);
// In another thread
let loaded = reference.load(Ordering::Relaxed);
assert_eq!(loaded, &b);
assert!(core::ptr::eq(loaded, &b));
use ref_swap::OptionRefSwap;
use core::sync::atomic::Ordering;
let a = 10;
let b = 20;
let reference = OptionRefSwap::new(None);
// In another thread
let loaded = reference.load(Ordering::Relaxed);
assert_eq!(loaded, None);
reference.store(Some(&b), Ordering::Relaxed);
// In another thread
let loaded = reference.load(Ordering::Relaxed);
assert_eq!(loaded, Some(&b));
assert!(core::ptr::eq(loaded.unwrap(), &b));
reference.store(Some(&a), Ordering::Relaxed);
// In another thread
let loaded = reference.load(Ordering::Relaxed);
assert_eq!(loaded, Some(&a));
assert!(core::ptr::eq(loaded.unwrap(), &a));
This project is licensed under the GNU Lesser General Public License (LGPL)
version 3. Configuration files and examples are licensed under the
CC0 1.0 license. For more information, see the license header in
each file. You can find a copy of the license texts in the
LICENSES
directory.
This project complies with version 3.0 of the REUSE specification.