pub struct RestoreOnDrop<T: 'static> { /* private fields */ }Expand description
A drop guard that restores a ContextCell to a previously-saved
value when dropped.
WideLogGuard owns one of these instead of a raw *mut T. When the
guard is dropped, the cell is restored to its prior state. If the user
calls std::mem::forget on the guard, the restoration is silently
skipped — this is a known limitation, documented on the
WideLogGuard rustdoc. A future release may use a leak-detection
fallback (e.g. a thread-local “current owner” pointer) to recover
safety even on forget.
§Safety contract
cell must be a thread-local or task-local ContextCell<T> with a
'static lifetime (i.e. defined in a thread_local! or
task_local! block). The previous pointer prev is treated as an
opaque value to be restored verbatim; it is never dereferenced.
This type is Send + Sync unconditionally because the only state is
a &'static ContextCell<T> (which is Send + Sync) and a raw
pointer that is never dereferenced by this type.
Implementations§
Source§impl<T: 'static> RestoreOnDrop<T>
impl<T: 'static> RestoreOnDrop<T>
Sourcepub const unsafe fn new(cell: &'static ContextCell<T>, prev: *mut T) -> Self
pub const unsafe fn new(cell: &'static ContextCell<T>, prev: *mut T) -> Self
Build a restore guard for cell. The cell will be restored to
prev when this value is dropped.
§Safety
cell must be a thread-local or task-local ContextCell<T>
with 'static lifetime. prev is the value previously returned
by ContextCell::replace (or null for the initial state).