LocalReader

Struct LocalReader 

Source
pub struct LocalReader<T: 'static> { /* private fields */ }
Expand description

A reader thread’s local version state.

Each reader thread should create exactly one LocalReader via SwmrCell::local(). It is !Sync (due to Cell) and must be stored per-thread.

The LocalReader is used to:

  • Pin the thread to the current version via pin().
  • Obtain a PinGuard that protects access to values and can be dereferenced.

Thread Safety: LocalReader is not Sync and must be used by only one thread.

读者线程的本地版本状态。 每个读者线程应该通过 SwmrCell::local() 创建恰好一个 LocalReader。 它是 !Sync 的(因为 Cell),必须在每个线程中存储。 LocalReader 用于:

  • 通过 pin() 将线程钉住到当前版本。
  • 获取保护对值访问的 PinGuard,可以解引用来读取值。 线程安全性LocalReader 不是 Sync 的,必须仅由一个线程使用。

Implementations§

Source§

impl<T: 'static> LocalReader<T>

Source

pub fn is_pinned(&self) -> bool

Pin this thread to the current version.

Returns a PinGuard that keeps the thread pinned for its lifetime. The guard can be dereferenced to access the current value.

Reentrancy: This method is reentrant. Multiple calls can be nested, and the thread remains pinned until all returned guards are dropped. You can also clone a guard to create additional references: let guard2 = guard1.clone();

Example:

let local = cell.local();
let guard1 = local.pin();
let value = *guard1;  // Dereference to read
let guard2 = local.pin();  // Reentrant call
let guard3 = guard1.clone();     // Clone for nested scope
// Thread remains pinned until all three guards are dropped

While pinned, the thread is considered “active” at a particular version, and the garbage collector will not reclaim data from that version.

将此线程钉住到当前版本。

返回一个 PinGuard,在其生命周期内保持线程被钉住。 可以解引用该守卫来访问当前值。

可重入性:此方法是可重入的。多个调用可以嵌套,线程在所有返回的守卫被 drop 之前保持被钉住。 你也可以克隆一个守卫来创建额外的引用:let guard2 = guard1.clone();

示例

let local = cell.local();
let guard1 = local.pin();
let value = *guard1;  // 解引用来读取
let guard2 = local.pin();  // 可重入调用
let guard3 = guard1.clone();     // 克隆用于嵌套作用域
// 线程保持被钉住直到所有三个守卫被 drop

当被钉住时,线程被认为在特定版本“活跃“,垃圾回收器不会回收该版本的数据。 Check if this reader is currently pinned.

检查此读者当前是否被 pin。

Source

pub fn version(&self) -> usize

Get the current global version.

Note: This returns the global version, not the pinned version. To get the pinned version, use PinGuard::version().

获取当前全局版本。 注意:这返回全局版本,而不是 pin 的版本。 要获取 pin 的版本,请使用 PinGuard::version()

Source

pub fn pin(&self) -> PinGuard<'_, T>

Source

pub fn share(&self) -> SwmrReader<T>

Create a new SwmrReader from this LocalReader.

SwmrReader is Sync + Clone and acts as a factory for LocalReaders. This is equivalent to calling swmr_cell.reader(), but using the LocalReader’s reference to the shared state.

从此 LocalReader 创建一个新的 SwmrReaderSwmrReaderSync + Clone 的,充当 LocalReader 的工厂。 这相当于调用 swmr_cell.reader(),但使用 LocalReader 对共享状态的引用。

Source

pub fn into_swmr(self) -> SwmrReader<T>

Convert this LocalReader into a SwmrReader.

This consumes the LocalReader and returns a SwmrReader that can be sent to another thread to create new LocalReaders.

将此 LocalReader 转换为 SwmrReader。 这会消耗 LocalReader 并返回一个 SwmrReader, 该 SwmrReader 可以发送到另一个线程以创建新 LocalReader

Trait Implementations§

Source§

impl<T: 'static> Clone for LocalReader<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: 'static> Debug for LocalReader<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for LocalReader<T>

§

impl<T> !RefUnwindSafe for LocalReader<T>

§

impl<T> Send for LocalReader<T>

§

impl<T> !Sync for LocalReader<T>

§

impl<T> Unpin for LocalReader<T>

§

impl<T> UnwindSafe for LocalReader<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.