pub struct PRefCell<T: PSafe + ?Sized, A: MemPool> { /* private fields */ }
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

Creates a new instance of PRefCell with the given value

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 = Allocator::open_no_root("foo.pool", O_CF);
     
Allocator::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 = Allocator::open_no_root("foo.pool", O_CF);
     
Allocator::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 = Allocator;
 
let root = P::open::<PRefCell<i32,P>>("foo.pool", O_CF).unwrap();
 
unsafe {
    let mut data = root.as_mut();
    *data = 20;
}
 

Returns an immutable reference of the inner value

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

Returns a clone of the underlying data

Examples
use corundum::alloc::heap::*;

Heap::transaction(|j| {
    let cell = Pbox::new(PRefCell::new(5), j);
     
    assert_eq!(cell.read(), 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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Crates a new PRefCell

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

Performs copy-assignment from source. Read more

Crates a new PRefCell and drops the Ref

After calling this function, the Ref won’t be available anymore. It will be possible to borrow the PRefCell mutably. The new PRefCell has a new location with the same data.

Crates a new PRefCell and drops the Ref

After calling this function, the Ref won’t be available anymore. It will be possible to borrow the PRefCell mutably. The new PRefCell has a new location with the same data.

Crates a new PRefCell

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests greater than or equal to (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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.