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
Dropcode 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>
impl<'a, T: 'static> ScopedRefGuard<'a, T>
Trait Implementations§
Source§impl<'a, T: Debug + 'static> Debug for ScopedRefGuard<'a, T>
impl<'a, T: Debug + 'static> Debug for ScopedRefGuard<'a, T>
Source§impl<'a, T> Deref for ScopedRefGuard<'a, T>
impl<'a, T> Deref for ScopedRefGuard<'a, T>
Source§impl<'a, T: 'static> Drop for ScopedRefGuard<'a, T>
impl<'a, T: 'static> Drop for ScopedRefGuard<'a, T>
Source§impl<'a, T: Hash + 'static> Hash for ScopedRefGuard<'a, T>
impl<'a, T: Hash + 'static> Hash for ScopedRefGuard<'a, T>
Source§impl<'a, T: Ord + 'static> Ord for ScopedRefGuard<'a, T>
impl<'a, T: Ord + 'static> Ord for ScopedRefGuard<'a, T>
Source§fn cmp(&self, other: &ScopedRefGuard<'a, T>) -> Ordering
fn cmp(&self, other: &ScopedRefGuard<'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 ScopedRefGuard<'a, T>
impl<'a, T: PartialEq + 'static> PartialEq for ScopedRefGuard<'a, T>
Source§impl<'a, T: PartialOrd + 'static> PartialOrd for ScopedRefGuard<'a, T>
impl<'a, T: PartialOrd + 'static> PartialOrd for ScopedRefGuard<'a, T>
impl<'a, T: Eq + 'static> Eq for ScopedRefGuard<'a, T>
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> 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