pub struct LCellEnvironment<'k, T: Opaque + 'k> { /* private fields */ }
Expand description
A self-referential environment of LCell
s, 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,
impl<'k, T> LCellEnvironment<'k, T>where
T: Opaque,
Sourcepub fn new_with<F>(f: F) -> Self
pub fn new_with<F>(f: F) -> Self
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,
impl<'k, T> LCellEnvironment<'k, T>where
T: Opaque,
Sourcepub fn open_rw<'i, F, R>(self: Pin<&'i mut Self>, f: F) -> R
pub fn open_rw<'i, F, R>(self: Pin<&'i mut Self>, f: F) -> 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());
}
);
}
Sourcepub fn open_ro<'i, F, R>(self: Pin<&'i Self>, f: F) -> R
pub fn open_ro<'i, F, R>(self: Pin<&'i Self>, f: F) -> 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>
impl<'k, T> RefUnwindSafe for LCellEnvironment<'k, T>
impl<'k, T> Send for LCellEnvironment<'k, T>
impl<'k, T> Sync for LCellEnvironment<'k, T>
impl<'k, T> !Unpin for LCellEnvironment<'k, T>
impl<'k, T> UnwindSafe for LCellEnvironment<'k, T>
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