Struct LCellEnvironment

Source
pub struct LCellEnvironment<'k, T: Opaque + 'k> { /* private fields */ }
Expand description

A self-referential environment of LCells, with zero runtime cost.

Wrapper around Holder. Like Holder, the lifetime may be anything.

§Examples

use qcell::LCell;
 
use selfref::srce::LCellEnvironment;
use selfref::opaque;

struct Foo<'a> {
    foo: LCell<'a, Option<&'a Foo<'a>>>,
}

struct FooKey;
opaque! {
    impl Opaque for FooKey {
        type Kind<'a> = Foo<'a>;
    }
}

fn main() {
    // We can use a closure here, but we need to give the compiler a hint.
    let holder = LCellEnvironment::<'_, FooKey>::new_with(
        |foo| foo.build(Foo { foo: LCell::new(Default::default()) })
    );
}

Implementations§

Source§

impl<'k, T> LCellEnvironment<'k, T>
where T: Opaque,

Source

pub fn new_with<F>(f: F) -> Self
where F: for<'a> FnOnce(&mut Builder<'a, T>), T::Kind<'k>: Sized,

Creates a new LCellEnvironment.

Works exactly like Holder::new_with.

§Examples
use qcell::LCell;
 
use selfref::srce::LCellEnvironment;
use selfref::opaque;

struct Foo<'a> {
    foo: LCell<'a, Option<&'a Foo<'a>>>,
}

struct FooKey;
opaque! {
    impl Opaque for FooKey {
        type Kind<'a> = Foo<'a>;
    }
}

fn main() {
    // We can use a closure here, but we need to help the compiler.
    let holder = LCellEnvironment::<'_, FooKey>::new_with(
        |foo| foo.build(Foo { foo: LCell::new(Default::default()) })
    );
}
Source§

impl<'k, T> LCellEnvironment<'k, T>
where T: Opaque,

Source

pub fn open_rw<'i, F, R>(self: Pin<&'i mut Self>, f: F) -> R
where F: for<'a, 'id> FnOnce(&'a mut LCellOwner<'id>, OperateIn<'id, T>) -> R,

Opens this LCellEnvironment for reading and writing (mutable).

§Examples
use qcell::LCell;
 
use selfref::srce::LCellEnvironment;
use selfref::opaque;

struct Foo<'a> {
    foo: LCell<'a, Option<&'a Foo<'a>>>,
}

struct FooKey;
opaque! {
    impl Opaque for FooKey {
        type Kind<'a> = Foo<'a>;
    }
}

fn main() {
    let mut holder = Box::pin(LCellEnvironment::<'_, FooKey>::new_with(
        |foo| foo.build(Foo { foo: LCell::new(Default::default()) })
    ));
    // Actually making our Foo refer to itself.
    holder.as_mut().open_rw(
        |owner, foo| {
            *owner.rw(&foo.foo) = Some(foo.get_ref());
        }
    );
}
Source

pub fn open_ro<'i, F, R>(self: Pin<&'i Self>, f: F) -> R
where F: for<'a, 'id> FnOnce(&'a LCellOwner<'id>, OperateIn<'id, T>) -> R,

Opens this LCellEnvironment for reading only (shared).

§Examples
use qcell::LCell;
 
use selfref::srce::LCellEnvironment;
use selfref::opaque;

struct Foo<'a> {
    foo: LCell<'a, Option<&'a Foo<'a>>>,
}

struct FooKey;
opaque! {
    impl Opaque for FooKey {
        type Kind<'a> = Foo<'a>;
    }
}

fn main() {
    let holder = Box::pin(LCellEnvironment::<'_, FooKey>::new_with(
        |foo| foo.build(Foo { foo: LCell::new(Default::default()) })
    ));
    holder.as_ref().open_ro(
        |owner, foo| {
            // can't write to it
            assert!(owner.ro(&foo.foo).is_none());
        }
    );
}

Auto Trait Implementations§

§

impl<'k, T> Freeze for LCellEnvironment<'k, T>
where <T as Opaque>::Kind<'k>: Freeze,

§

impl<'k, T> RefUnwindSafe for LCellEnvironment<'k, T>
where <T as Opaque>::Kind<'k>: RefUnwindSafe,

§

impl<'k, T> Send for LCellEnvironment<'k, T>
where <T as Opaque>::Kind<'k>: Send,

§

impl<'k, T> Sync for LCellEnvironment<'k, T>
where <T as Opaque>::Kind<'k>: Sync,

§

impl<'k, T> !Unpin for LCellEnvironment<'k, T>

§

impl<'k, T> UnwindSafe for LCellEnvironment<'k, T>
where <T as Opaque>::Kind<'k>: UnwindSafe,

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<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.