pub struct SwmrCell<T: 'static> { /* private fields */ }Expand description
A single-writer, multi-reader cell with version-based garbage collection.
SwmrCell provides safe concurrent access where one writer can update the value
and multiple readers can read it concurrently. Readers access the value by
creating a LocalReader and pinning it.
单写多读单元,带有基于版本的垃圾回收。
SwmrCell 提供安全的并发访问,其中一个写入者可以更新值,
多个读者可以并发读取它。读者通过创建 LocalReader 并 pin 来访问值。
Implementations§
Source§impl<T: 'static> SwmrCell<T>
impl<T: 'static> SwmrCell<T>
Sourcepub fn new(data: T) -> Self
pub fn new(data: T) -> Self
Create a new SWMR cell with default settings and the given initial value.
使用默认设置和给定的初始值创建一个新的 SWMR 单元。
Sourcepub fn builder() -> SwmrCellBuilder<T>
pub fn builder() -> SwmrCellBuilder<T>
Returns a builder for configuring the SWMR cell.
返回用于配置 SWMR 单元的构建器。
Sourcepub fn local(&self) -> LocalReader<T>
pub fn local(&self) -> LocalReader<T>
Create a new LocalReader for reading.
Each thread should create its own LocalReader and reuse it.
LocalReader is !Sync and should not be shared between threads.
创建一个新的 LocalReader 用于读取。
每个线程应该创建自己的 LocalReader 并重复使用。
LocalReader 是 !Sync 的,不应在线程之间共享。
Sourcepub fn reader(&self) -> SwmrReader<T>
pub fn reader(&self) -> SwmrReader<T>
Create a new SwmrReader that can be shared across threads.
SwmrReader is Sync + Clone and acts as a factory for LocalReaders.
This is useful for distributing reader creation capability to other threads.
创建一个新的 SwmrReader,可以在线程之间共享。
SwmrReader 是 Sync + Clone 的,充当 LocalReader 的工厂。
这对于将读者创建能力分发给其他线程很有用。
Sourcepub fn store(&mut self, data: T)
pub fn store(&mut self, data: T)
Store a new value, making it visible to readers. The old value is retired and will be garbage collected.
This operation increments the global version.
存储新值,使其对读者可见。 旧值已退休,将被垃圾回收。 此操作会增加全局版本。
Sourcepub fn previous(&self) -> Option<&T>
pub fn previous(&self) -> Option<&T>
Get a reference to the previously stored value, if any.
Returns None if no previous value exists (i.e., only the initial value has been stored).
Note: The previous value is guaranteed not to be garbage collected because
collect() uses safety_limit = current_version - 2, which always preserves
the most recently retired value (version = current_version - 1).
This is useful for comparing the current value with the previous one, or for implementing undo/rollback logic.
获取上一个存储值的引用(如果存在)。
如果不存在上一个值(即只存储了初始值),则返回 None。
注意:上一个值保证不会被垃圾回收,因为 collect() 使用 safety_limit = current_version - 2,
这始终保留最近退休的值(版本 = current_version - 1)。
这对于将当前值与上一个值进行比较,或实现撤销/回滚逻辑很有用。
§Example
use swmr_cell::SwmrCell;
let mut cell = SwmrCell::new(1);
assert!(cell.previous().is_none()); // No previous value yet
cell.store(2);
assert_eq!(cell.previous(), Some(&1)); // Previous value is 1
cell.store(3);
assert_eq!(cell.previous(), Some(&2)); // Previous value is 2Sourcepub fn get(&self) -> &T
pub fn get(&self) -> &T
Get a reference to the current value (writer-only, no pinning required).
This is only accessible from the writer thread since SwmrCell is !Sync.
获取当前值的引用(仅写者可用,无需 pin)。
这只能从写者线程访问,因为 SwmrCell 是 !Sync 的。
Sourcepub fn update<F>(&mut self, f: F)
pub fn update<F>(&mut self, f: F)
Update the value using a closure.
The closure receives the current value and should return the new value.
This is equivalent to cell.store(f(cell.get().clone())) but more ergonomic.
使用闭包更新值。
闭包接收当前值并应返回新值。
这相当于 cell.store(f(cell.get().clone())) 但更符合人体工程学。
Sourcepub fn version(&self) -> usize
pub fn version(&self) -> usize
Get the current global version.
The version is incremented each time store() or replace() is called.
获取当前全局版本。
每次调用 store() 或 replace() 时版本会增加。
Sourcepub fn garbage_count(&self) -> usize
pub fn garbage_count(&self) -> usize
Get the number of retired objects waiting for garbage collection.
获取等待垃圾回收的已退休对象数量。