pub struct Lifelink<C: Ctor> { /* private fields */ }
Expand description
A 'static
handle through which a value with a covariant lifetime parameter can
temporarily be accessed.
Lifelink<C>
implements Send
and Sync
when the wrapped values produced by
C
are Send
.
See Lifelink::new
for an example.
Implementations§
source§impl<C: Ctor> Lifelink<C>
impl<C: Ctor> Lifelink<C>
sourcepub unsafe fn new<'a>(thing: C::Ty<'a>) -> (Lifelink<C>, Deathtouch<'a, C>)
pub unsafe fn new<'a>(thing: C::Ty<'a>) -> (Lifelink<C>, Deathtouch<'a, C>)
Create a pair of Lifelink
and Deathtouch
values wrapping thing
, which
will be kept alive and accessible through Lifelink
until the Deathtouch
is dropped.
The safety of this function depends on the resulting Deathtouch
value being
unwrapped or dropped. For a safe way to construct Lifelink
in exchange for
the ability to access Deathtouch
, see the lifelink!
macro.
Safety
C::Ty
values must be covariant over the lifetime parameter, and the resulting
Deathtouch
value must be unwrapped or dropped (i.e. not forgotten). For more
details on the latter requirement, see https://github.com/chitoyuu/lifelink/issues/2.
Example
use std::thread::spawn;
use std::sync::atomic::{AtomicUsize, Ordering};
use lifelink::{Lifelink, RefCtor};
let answer = AtomicUsize::new(0);
let (mut lifelink, deathtouch) = unsafe { Lifelink::<RefCtor<AtomicUsize>>::new(&answer) };
{
let guard = lifelink.get().unwrap();
assert_eq!(0, guard.load(Ordering::Relaxed));
guard.store(42, Ordering::Release);
}
assert_eq!(42, deathtouch.unwrap().load(Ordering::Acquire));