#reference #atomic #no-std #arc-swap

no-std ref-swap

AtomicPtr的安全包装

3个版本

0.1.2 2023年6月6日
0.1.1 2023年6月5日
0.1.0 2023年5月30日

1861编码

Download history 299/week @ 2024-03-13 463/week @ 2024-03-20 336/week @ 2024-03-27 446/week @ 2024-04-03 315/week @ 2024-04-10 179/week @ 2024-04-17 267/week @ 2024-04-24 287/week @ 2024-05-01 162/week @ 2024-05-08 204/week @ 2024-05-15 308/week @ 2024-05-22 255/week @ 2024-05-29 324/week @ 2024-06-05 139/week @ 2024-06-12 236/week @ 2024-06-19 330/week @ 2024-06-26

每月下载1,073次

Apache-2.0 OR MIT

18KB
235

Ref-Swap

AtomicPtr的安全包装。它不是交换指针,而是使用引用和生命周期进行操作,允许安全的API。提供了两种版本

使用引用

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

许可证

该项目遵循GNU Lesser General Public License (LGPL) 版本 3。配置文件和示例遵循CC0 1.0许可证。有关更多信息,请参阅每个文件中的许可证标题。您可以在LICENSES目录中找到许可证文本。

该项目符合REUSE规范版本3.0

无运行时依赖