ScopeGuard

Struct ScopeGuard 

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

A reference with lifetime 'a that can be lifted to a reference with a 'static lifetime (Scoped). Runtime checks are used to ensure that no derived Scoped exists when this ScopeGuard is dropped.

use scoped_static::ScopeGuard;

#[tokio::main]
async fn main() {
    let concrete_value = Box::new(1.0);
    let ref_value = &concrete_value;
    let guard = ScopeGuard::new(ref_value);
    let lifted = guard.lift();
    tokio::spawn(async move {
        // Lifted is 'static so it can be moved into this closure that needs 'static
        let value = **lifted + 1.0;
        assert_eq!(value, 2.0);
        // `lifted` is dropped here
    })
    .await
    .unwrap();
   // `guard` is dropped here
}

If a ScopeGuard is dropped while any derived Scoped exist, then it will abort the whole program (instead of panic). This is because Scoped could exist on another thread and be unaffected by the panic or the panic could be recovered from. This could lead to undefined behavior.

UNDEFINED BEHAVIOR: It may cause undefined behavior to forget this value (std::mem::forget(guard)) - the Drop code must run to prevent undefined behavior.

Implementations§

Source§

impl<'a, T: 'static> ScopeGuard<'a, T>

Source

pub fn new(value: &'a T) -> Self

Source

pub fn lift(&self) -> Scoped<T>

Lifts this reference with lifetime 'a into 'static and relies on runtime checks to ensure safety.

Trait Implementations§

Source§

impl<'a, T: Debug + 'static> Debug for ScopeGuard<'a, T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Deref for ScopeGuard<'a, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, T: 'static> Drop for ScopeGuard<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T: Hash + 'static> Hash for ScopeGuard<'a, T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T: Ord + 'static> Ord for ScopeGuard<'a, T>

Source§

fn cmp(&self, other: &ScopeGuard<'a, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a, T: PartialEq + 'static> PartialEq for ScopeGuard<'a, T>

Source§

fn eq(&self, other: &ScopeGuard<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T: PartialOrd + 'static> PartialOrd for ScopeGuard<'a, T>

Source§

fn partial_cmp(&self, other: &ScopeGuard<'a, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T: Eq + 'static> Eq for ScopeGuard<'a, T>

Source§

impl<'a, T: 'static> StructuralPartialEq for ScopeGuard<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for ScopeGuard<'a, T>

§

impl<'a, T> RefUnwindSafe for ScopeGuard<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for ScopeGuard<'a, T>
where T: Sync,

§

impl<'a, T> Sync for ScopeGuard<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for ScopeGuard<'a, T>

§

impl<'a, T> UnwindSafe for ScopeGuard<'a, 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<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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.