Struct corundum::cell::PRefCell [−][src]
Expand description
A persistent memory location with safe interior mutability and dynamic borrow checking
This is one of the safe ways to provide interior mutability for pointer wrappers. It takes a log, if it was not already taken, before exposing the mutable reference to the protected data.
To borrow the value immutably, borrow()
can be used. Its
return value is a Ref<T>
. The function
borrow_mut()
returns the inner value wrapped in
RefMut<T>
. The borrowing rules is checked
dynamically when the user tries to borrow the value. It panics if any of
the following situation happens:
- Borrowing the value mutably while it was already borrowed immutably
- Borrowing the value mutably twice
- Borrowing the value immutably while it was already borrowed mutably
It does not implement Sync
, so it is not possible to share PRefCell
between threads. To provide thread-safe interior mutability, use
PMutex
.
PRefCell
is an alias name in the pool module for PRefCell
.
Implementations
Replaces the wrapped value with a new one, returning the old value, without deinitializing either one.
This function corresponds to std::mem::replace
.
Panics
Panics if the value is currently borrowed.
Examples
use corundum::alloc::heap::*; Heap::transaction(|j| { let cell = Pbox::new(PRefCell::new(5), j); let old_value = cell.replace(6, j); assert_eq!(old_value, 5); assert_eq!(*cell.borrow(), 6); }).unwrap();
Replaces the wrapped value with a new one computed from f
, returning
the old value, without deinitializing either one.
Panics
Panics if the value is currently borrowed.
Examples
use corundum::alloc::heap::*; Heap::transaction(|j| { let cell = Pbox::new(PRefCell::new(5), j); let old_value = cell.replace_with(j, |&mut old| old + 1); assert_eq!(old_value, 5); assert_eq!(*cell.borrow(), 6); }).unwrap();
Swaps the wrapped value of self
with the wrapped value of other
,
without deinitializing either one.
This function corresponds to std::mem::swap
.
Panics
Panics if the value in either RefCell
is currently borrowed.
Examples
use corundum::default::*; let _pool = BuddyAlloc::open_no_root("foo.pool", O_CF); BuddyAlloc::transaction(|j| { let c1 = Pbox::new(PRefCell::new(5i32), j); let c2 = Pbox::new(PRefCell::new(10i32), j); c1.swap(&c2, j); assert_eq!(10, c1.take(j)); assert_eq!(5, c2.take(j)); }).unwrap();
Takes a log and returns a mutable reference to the underlying data.
This call borrows the UnsafeCell
mutably (at compile-time) which
guarantees that we possess the only reference.
Examples
use corundum::default::*; let _pool = BuddyAlloc::open_no_root("foo.pool", O_CF); BuddyAlloc::transaction(|j| { let c1 = Pbox::new(PRefCell::new(5i32), j); let c2 = Pbox::new(PRefCell::new(10i32), j); c1.swap(&c2, j); assert_eq!(10, *c1.borrow()); assert_eq!(5, *c2.borrow()); }).unwrap();
Returns a mutable reference to the underlying data without logging
Safety
This function violates borrow rules as it allows multiple mutable references.
Examples
use corundum::default::*; use corundum::cell::PRefCell; type P = BuddyAlloc; let root = P::open::<PRefCell<i32,P>>("foo.pool", O_CF).unwrap(); unsafe { let mut data = root.as_mut(); *data = 20; }
Immutably borrows from an owned value.
Examples
use corundum::alloc::heap::*; use corundum::boxed::Pbox; Heap::transaction(|j| { let cell = Pbox::new(PRefCell::new(5), j); assert_eq!(*cell.borrow(), 5); }).unwrap();
Takes the value of the cell, leaving Default::default()
in its place.
Examples
use corundum::alloc::heap::*; Heap::transaction(|j| { let c = Pbox::new(PRefCell::new(5), j); let five = c.take(j); assert_eq!(five, 5); assert_eq!(*c.borrow(), 0); }).unwrap();
Mutably borrows from an owned value.
It returns a RefMut
type for interior mutability which takes a log of
data when dereferenced mutably. This method requires accessing current
journal which is provided in transaction
.
Examples
use corundum::alloc::heap::*; use corundum::boxed::Pbox; let cell=Heap::transaction(|j| { let cell = Pbox::new(PRefCell::new(5), j); { let mut cell = cell.borrow_mut(j); *cell = 10; } assert_eq!(*cell.borrow(), 10); }).unwrap();
Returns a LogNonNull
pointer to the data
Safety
LogNonNull
does not dynamically check the borrowing rules. Also, it
may outlive the data, leading to a segmentation fault. It is not
recommended to use this function without necessary manual checks.
Returns a NonNull
pointer to the data
Trait Implementations
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Safe to transfer between thread boundaries
Not safe for thread data sharing
Auto Trait Implementations
impl<T: ?Sized, A> LooseTxInUnsafe for PRefCell<T, A> where
A: LooseTxInUnsafe,
T: LooseTxInUnsafe,
Blanket Implementations
Mutably borrows from an owned value. Read more