3 releases

0.1.2 Jun 6, 2023
0.1.1 Jun 5, 2023
0.1.0 May 30, 2023

#1664 in Encoding

Download history 363/week @ 2024-08-30 232/week @ 2024-09-06 362/week @ 2024-09-13 410/week @ 2024-09-20 547/week @ 2024-09-27 422/week @ 2024-10-04 433/week @ 2024-10-11 709/week @ 2024-10-18 623/week @ 2024-10-25 394/week @ 2024-11-01 676/week @ 2024-11-08 687/week @ 2024-11-15 565/week @ 2024-11-22 406/week @ 2024-11-29 530/week @ 2024-12-06 494/week @ 2024-12-13

2,078 downloads per month

Apache-2.0 OR MIT

18KB
235 lines

Ref-Swap

Safe wrapper around AtomicPtr. Instead of swapping a pointer, it works with references and lifetimes, allowing a safe API. Two versions are provided:

With references

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));

With optionnal references

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));

License

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.

No runtime deps