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§
Trait Implementations§
Source§impl<'a, T: Debug + 'static> Debug for ScopeGuard<'a, T>
impl<'a, T: Debug + 'static> Debug for ScopeGuard<'a, T>
Source§impl<'a, T> Deref for ScopeGuard<'a, T>
impl<'a, T> Deref for ScopeGuard<'a, T>
Source§impl<'a, T: 'static> Drop for ScopeGuard<'a, T>
impl<'a, T: 'static> Drop for ScopeGuard<'a, T>
Source§impl<'a, T: Hash + 'static> Hash for ScopeGuard<'a, T>
impl<'a, T: Hash + 'static> Hash for ScopeGuard<'a, T>
Source§impl<'a, T: Ord + 'static> Ord for ScopeGuard<'a, T>
impl<'a, T: Ord + 'static> Ord for ScopeGuard<'a, T>
Source§fn cmp(&self, other: &ScopeGuard<'a, T>) -> Ordering
fn cmp(&self, other: &ScopeGuard<'a, T>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<'a, T: PartialEq + 'static> PartialEq for ScopeGuard<'a, T>
impl<'a, T: PartialEq + 'static> PartialEq for ScopeGuard<'a, T>
Source§impl<'a, T: PartialOrd + 'static> PartialOrd for ScopeGuard<'a, T>
impl<'a, T: PartialOrd + 'static> PartialOrd for ScopeGuard<'a, T>
impl<'a, T: Eq + 'static> Eq for ScopeGuard<'a, T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more