Struct qcell::LCellOwner

source ·
pub struct LCellOwner<'id> { /* private fields */ }
Expand description

Borrowing-owner of zero or more LCell instances.

Use LCellOwner::scope(|owner| ...) to create an instance of this type. The key piece of Rust syntax that enables this is for<'id>. This allows creating an invariant lifetime within a closure, which is different to any other Rust lifetime thanks to the techniques explained in various places: section 6.3 of this thesis from Gankra (formerly Gankro), this Reddit post, and this Rust playground example. Also see this Reddit comment and its linked playground code.

Alternatively, if the generativity feature is enabled, the generativity crate can be used to create an owner as follows: make_guard!(guard); let mut owner = LCellOwner::new(guard);. However note that the Rust compiler error messages may be more confusing with generativity if you make a mistake and use the wrong owner for a cell.

Some history: GhostCell by pythonesque predates the creation of LCell, and inspired it. Discussion of GhostCell on Reddit showed that a lifetime-based approach to cells was feasible, but unfortunately the ghost_cell.rs source didn’t seem to be available under a community-friendly licence. So I went back to first principles and created LCell from TCell code, combined with invariant lifetime code derived from the various community sources that predate GhostCell. Later Send and Sync support for LCell was contributed independently.

See also crate documentation.

Implementations§

source§

impl<'id> LCellOwner<'id>

source

pub fn scope<F>(f: F)where F: for<'scope_id> FnOnce(LCellOwner<'scope_id>),

Create a new LCellOwner, with a new lifetime, that exists only within the scope of the execution of the given closure call. If two scope calls are nested, then the two owners get different lifetimes.

use qcell::{LCellOwner, LCell};
LCellOwner::scope(|owner| {
    let cell = LCell::new(100);
    assert_eq!(cell.ro(&owner), &100);
})
source

pub fn new(_guard: Guard<'id>) -> Self

Available on crate feature generativity only.

Create a new LCellOwner with a unique lifetime from a Guard.

use qcell::{generativity::make_guard, LCellOwner, LCell};
make_guard!(guard);
let mut owner = LCellOwner::new(guard);
let cell = LCell::new(100);
assert_eq!(cell.ro(&owner), &100);
source

pub fn cell<T>(&self, value: T) -> LCell<'id, T>

Create a new cell owned by this owner instance. See also LCell::new.

source

pub fn ro<'a, T: ?Sized>(&'a self, lc: &'a LCell<'id, T>) -> &'a T

Borrow contents of a LCell immutably (read-only). Many LCell instances can be borrowed immutably at the same time from the same owner.

source

pub fn rw<'a, T: ?Sized>(&'a mut self, lc: &'a LCell<'id, T>) -> &'a mut T

Borrow contents of a LCell mutably (read-write). Only one LCell at a time can be borrowed from the owner using this call. The returned reference must go out of scope before another can be borrowed.

source

pub fn rw2<'a, T: ?Sized, U: ?Sized>( &'a mut self, lc1: &'a LCell<'id, T>, lc2: &'a LCell<'id, U> ) -> (&'a mut T, &'a mut U)

Borrow contents of two LCell instances mutably. Panics if the two LCell instances point to the same memory.

source

pub fn rw3<'a, T: ?Sized, U: ?Sized, V: ?Sized>( &'a mut self, lc1: &'a LCell<'id, T>, lc2: &'a LCell<'id, U>, lc3: &'a LCell<'id, V> ) -> (&'a mut T, &'a mut U, &'a mut V)

Borrow contents of three LCell instances mutably. Panics if any pair of LCell instances point to the same memory.

Auto Trait Implementations§

§

impl<'id> RefUnwindSafe for LCellOwner<'id>

§

impl<'id> Send for LCellOwner<'id>

§

impl<'id> Sync for LCellOwner<'id>

§

impl<'id> Unpin for LCellOwner<'id>

§

impl<'id> UnwindSafe for LCellOwner<'id>

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.