Skip to main content

RestoreOnDrop

Struct RestoreOnDrop 

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

Source

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).

Source

pub fn disarm(self) -> *mut T

Disable the restore-on-drop behavior. The cell is NOT restored. Returns the saved prev pointer so the caller can restore manually or discard it.

Trait Implementations§

Source§

impl<T: 'static> Drop for RestoreOnDrop<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: 'static> Send for RestoreOnDrop<T>

Source§

impl<T: 'static> Sync for RestoreOnDrop<T>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for RestoreOnDrop<T>

§

impl<T> !UnwindSafe for RestoreOnDrop<T>

§

impl<T> Freeze for RestoreOnDrop<T>

§

impl<T> Unpin for RestoreOnDrop<T>

§

impl<T> UnsafeUnpin for RestoreOnDrop<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<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.