Skip to main content

ContextCell

Struct ContextCell 

Source
pub struct ContextCell<T> { /* private fields */ }
Expand description

A thread-local cell storing a raw pointer to the current wide event.

This type provides safe methods for setting, getting, and saving/restoring the pointer, which is used by the generated guard to manage nested scopes.

§Safety Invariants

The raw pointer stored in this cell is sound because:

  1. The guard owns the event. The WideEvent<K> is a field of the guard, which is on the stack (or in the scope() future’s state). The guard outlives any code that calls current() within the same scope.
  2. Set on creation, cleared on drop. The guard’s new sets the pointer; its Drop restores the previous pointer (or null) before the inner ScopedGuard::Drop runs.
  3. Nested scopes save/restore the previous pointer via replace and the prev_ptr field on the guard.
  4. No cross-thread access. thread_local! means each thread has its own cell. For async, tokio::task_local! moves with the task across threads.

The &'static lifetime returned by accessors is a “valid for the duration of the guard” lifetime — the standard pattern used by thread_local accessors (e.g., tracing’s Span::current()).

Implementations§

Source§

impl<T> ContextCell<T>

Source

pub const fn new() -> Self

Create a new cell initialized to a null pointer.

Source

pub fn get_ptr(&self) -> *mut T

Returns the stored raw pointer (null if unset).

Source

pub fn replace(&self, ptr: *mut T) -> *mut T

Sets the stored pointer. Returns the previous pointer.

Source

pub fn restore(&self, ptr: *mut T)

Restores a previously saved pointer (typically from replace or clear).

Trait Implementations§

Source§

impl<T> Default for ContextCell<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Send for ContextCell<T>

Source§

impl<T> Sync for ContextCell<T>

Auto Trait Implementations§

§

impl<T> !Freeze for ContextCell<T>

§

impl<T> !RefUnwindSafe for ContextCell<T>

§

impl<T> Unpin for ContextCell<T>

§

impl<T> UnsafeUnpin for ContextCell<T>

§

impl<T> UnwindSafe for ContextCell<T>
where T: RefUnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.