ScopedRefGuard

Struct ScopedRefGuard 

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

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

use scoped_static::ScopedRefGuard;

#[tokio::main]
async fn main() {
    let concrete_value = Box::new(1.0);
    let ref_value = &concrete_value;
    let guard = unsafe { ScopedRefGuard::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 ScopedRefGuard is dropped while any derived ScopedRef exist, then it will abort the whole program (instead of panic). This is because ScopedRef 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 leak/forget this value (e.g. std::mem::forget, std::mem::ManuallyDrop, or Rc cycles, etc.)

  • the Drop code must run to prevent undefined behavior.

See scoped_static macro for a safe way to create.

Implementations§

Source§

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

Source

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

Creates a new ScopeGuard. See scoped_static for a safe way to create

Source

pub fn lift(&self) -> ScopedRef<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 ScopedRefGuard<'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 ScopedRefGuard<'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 ScopedRefGuard<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T: Hash + 'static> Hash for ScopedRefGuard<'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 ScopedRefGuard<'a, T>

Source§

fn cmp(&self, other: &ScopedRefGuard<'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 ScopedRefGuard<'a, T>

Source§

fn eq(&self, other: &ScopedRefGuard<'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 ScopedRefGuard<'a, T>

Source§

fn partial_cmp(&self, other: &ScopedRefGuard<'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 ScopedRefGuard<'a, T>

Source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<'a, T> UnwindSafe for ScopedRefGuard<'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.