Struct lifelink::Lifelink

source ·
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>

source

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));
source

pub fn get(&mut self) -> Option<Guard<'_, C>>

Returns a guard value that implements Deref to the wrapped value. Care must be taken to ensure that this value is dropped properly. Leaking the guard value may lead to a deadlock.

Trait Implementations§

Auto Trait Implementations§

§

impl<C> !RefUnwindSafe for Lifelink<C>

§

impl<C> Unpin for Lifelink<C>where <C as Ctor>::Ty<'static>: Unpin,

§

impl<C> !UnwindSafe for Lifelink<C>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.