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:
- The guard owns the event. The
WideEvent<K>is a field of the guard, which is on the stack (or in thescope()future’s state). The guard outlives any code that callscurrent()within the same scope. - Set on creation, cleared on drop. The guard’s
newsets the pointer; itsDroprestores the previous pointer (or null) before the innerScopedGuard::Dropruns. - Nested scopes save/restore the previous pointer via
replaceand theprev_ptrfield on the guard. - 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>
impl<T> ContextCell<T>
Sourcepub fn replace(&self, ptr: *mut T) -> *mut T
pub fn replace(&self, ptr: *mut T) -> *mut T
Sets the stored pointer. Returns the previous pointer.
Sourcepub fn restore(&self, ptr: *mut T)
pub fn restore(&self, ptr: *mut T)
Restores a previously saved pointer (typically from replace or clear).
Sourcepub unsafe fn deref_mut(&self) -> Option<&'static mut T>
pub unsafe fn deref_mut(&self) -> Option<&'static mut T>
Returns a &'static mut T reference to the event if the pointer is non-null.
§Safety
The caller must ensure the pointer is valid and that the referenced T
will not be accessed mutably from another reference for the duration of
the returned borrow. In practice, this is guaranteed by the guard’s
lifetime — the guard outlives all current() calls within its scope.