Struct corundum::cell::PCell[][src]

pub struct PCell<T: PSafe + ?Sized, A: MemPool> { /* fields omitted */ }
Expand description

A persistent mutable memory location with recoverability

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 updating the value.

Using get() function, you can obtain a copy of data. To update data, you can use set() which writes a log to the given journal before mutation.

It does not implement Sync, so it is not possible to share PCell between threads. To provide thread-safe interior mutability, use PMutex.

PCell is a compact version of PCell tha can be find in the pool module.

Implementations

Creates a new PCell containing the given value.

Examples

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

Heap::transaction(|j| {
    let c = PCell::new(5);
}).unwrap();

Sets the contained value.

Examples

use corundum::alloc::heap::*;
use corundum::boxed::Pbox;
use corundum::cell::PCell;

Heap::transaction(|j| {
    let c = Pbox::new(PCell::new(5), j);
    c.set(10, j);
}).unwrap();

Errors

If PCell is not in the persistent memory, it will raise an ‘invalid address’ error. To make sure that the PCell is in the persistent memory, use dynamic allocation using Pbox as shown above.

Swaps the values of two Cells.

Difference with std::mem::swap is that this function doesn’t require &mut reference. It takes a log of both sides, if required, and then swaps the values.

Examples

use corundum::default::*;

let _pool = BuddyAlloc::open_no_root("foo.pool", O_CF).unwrap();
     
BuddyAlloc::transaction(|j| {
    let c1 = Pbox::new(PCell::new(5i32), j);
    let c2 = Pbox::new(PCell::new(10i32), j);
    c1.swap(&c2, j);
    assert_eq!(10, c1.get());
    assert_eq!(5, c2.get());
}).unwrap();

Replaces the contained value, and returns it.

Examples

use corundum::alloc::heap::*;
use corundum::boxed::Pbox;
use corundum::cell::PCell;

Heap::transaction(|j| {
    let cell = Pbox::new(PCell::new(5), j);
    assert_eq!(cell.get(), 5);
    assert_eq!(cell.replace(10, j), 5);
    assert_eq!(cell.get(), 10);
}).unwrap();

Unwraps the value.

Examples

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

Heap::transaction(|j| {
    let c = PCell::new(5);
    let five = c.into_inner();

    assert_eq!(five, 5);
}).unwrap();

Returns a copy of the contained value.

Examples

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

Heap::transaction(|j| {
    let c = PCell::new(5);
    let five = c.get();
     
    assert_eq!(five, 5);
}).unwrap();

Updates the contained value using a function and returns the new value.

Examples

#![feature(cell_update)]

use corundum::alloc::heap::*;
use corundum::boxed::Pbox;

Heap::transaction(|j| {
    let c = Pbox::new(PCell::new(5), j);
    let new = c.update(|x| x + 1, j);

    assert_eq!(new, 6);
    assert_eq!(c.get(), 6);
}).unwrap();

Returns a mutable reference to the underlying data.

This call borrows PCell mutably (at compile-time) which guarantees that we possess the only reference.

Examples

use corundum::alloc::heap::*;
use corundum::boxed::Pbox;
use corundum::cell::PCell;

Heap::transaction(|j| {
    let mut c = Pbox::new(PCell::new(5), j);
    let mut n = c.get_mut(j);
    *n += 1;

    assert_eq!(c.get(), 6);
}).unwrap();

Returns a mutable reference to the underlying data without taking a log

Safety

This function violates borrow rules as it allows multiple mutable references.

Examples

use corundum::default::*;
use corundum::cell::PCell;
 
type P = BuddyAlloc;
 
let root = P::open::<PCell<i32,P>>("foo.pool", O_CF).unwrap();
 
unsafe {
    let mut data = root.as_mut();
    *data = 20;
}
 

Takes the value of the cell, leaving Default::default() in its place.

Examples

use corundum::alloc::heap::*;
use corundum::boxed::Pbox;

Heap::transaction(|j| {
    let c = Pbox::new(PCell::new(5), j);
    let five = c.take(j);

    assert_eq!(five, 5);
    assert_eq!(c.get(), 0);
}).unwrap();

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

Returns the “default value” for a type. Read more

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

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

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

Performs the conversion.

Performs the conversion.

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)

recently added

Uses borrowed data to replace owned data, usually by cloning. 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.