SwmrCell

Struct SwmrCell 

Source
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>

Source

pub fn new(data: T) -> Self

Create a new SWMR cell with default settings and the given initial value.

使用默认设置和给定的初始值创建一个新的 SWMR 单元。

Source

pub fn builder() -> SwmrCellBuilder<T>

Returns a builder for configuring the SWMR cell.

返回用于配置 SWMR 单元的构建器。

Source

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 的,不应在线程之间共享。

Source

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,可以在线程之间共享。 SwmrReaderSync + Clone 的,充当 LocalReader 的工厂。 这对于将读者创建能力分发给其他线程很有用。

Source

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.

存储新值,使其对读者可见。 旧值已退休,将被垃圾回收。 此操作会增加全局版本。

Source

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 2
Source

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 的。

Source

pub fn update<F>(&mut self, f: F)
where F: FnOnce(&T) -> T,

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())) 但更符合人体工程学。

Source

pub fn version(&self) -> usize

Get the current global version.

The version is incremented each time store() or replace() is called.

获取当前全局版本。 每次调用 store()replace() 时版本会增加。

Source

pub fn garbage_count(&self) -> usize

Get the number of retired objects waiting for garbage collection.

获取等待垃圾回收的已退休对象数量。

Source

pub fn collect(&mut self)

Manually trigger garbage collection. 手动触发垃圾回收。

Trait Implementations§

Source§

impl<T: Debug + 'static> Debug for SwmrCell<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default + 'static> Default for SwmrCell<T>

Source§

fn default() -> Self

Create a new SWMR cell with the default value.

使用默认值创建一个新的 SWMR 单元。

Source§

impl<T: 'static> From<T> for SwmrCell<T>

Source§

fn from(value: T) -> Self

Create a new SWMR cell from a value.

从一个值创建一个新的 SWMR 单元。

Auto Trait Implementations§

§

impl<T> Freeze for SwmrCell<T>

§

impl<T> RefUnwindSafe for SwmrCell<T>
where T: RefUnwindSafe,

§

impl<T> Send for SwmrCell<T>
where T: Send,

§

impl<T> Sync for SwmrCell<T>
where T: Sync,

§

impl<T> Unpin for SwmrCell<T>

§

impl<T> UnwindSafe for SwmrCell<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.