[][src]Struct swym::tcell::TCell

#[repr(C)]
pub struct TCell<T> { /* fields omitted */ }

A transactional memory location.

TCell stores an extra usize representing the current version of the memory.

The current value is stored directly in the TCell meaning it's not Boxed, Arc'ed, etc.

Methods

impl<T> TCell<T>[src]

pub const fn new(value: T) -> TCell<T>[src]

Construct a new TCell from an initial value.

This does not perform any memory allocation or synchronization.

Examples

use swym::{tcell::TCell, thread_key};

static ZERO: TCell<i32> = TCell::new(0);
assert_eq!(
    thread_key::get().read(|tx| Ok(ZERO.get(tx, Default::default())?)),
    0
);

pub fn into_inner(self) -> T[src]

Consumes this TCell, returning the underlying data.

Examples

use swym::tcell::TCell;

let x = TCell::new(42);
assert_eq!(x.into_inner(), 42);

pub fn borrow_mut(&mut self) -> &mut T[src]

Returns a mutable reference to the underlying data.

Since this call borrows the TCell mutably, no synchronization needs to take place. The mutable borrow statically guarantees no other threads are accessing this data.

Examples

use swym::tcell::TCell;

let mut x = TCell::new("hello");
*x.borrow_mut() = "world";
assert_eq!(*x.borrow_mut(), "world");

pub fn view<'tcell, Tx>(&'tcell self, transaction: Tx) -> View<'tcell, T, Tx> where
    Tx: Deref,
    Tx::Target: Read<'tcell> + Sized
[src]

impl<T: Borrow> TCell<T>[src]

pub fn borrow<'tx, 'tcell>(
    &'tcell self,
    tx: &'tx impl Read<'tcell>,
    ordering: Ordering
) -> Result<Ref<'tx, T>, Error>
[src]

Gets a reference to the contained value using the specified memory Ordering.

Statically requires that the TCell outlives the current transaction.

Errors

If another thread has written to this TCell during the current transaction, an error is returned.

Examples

use swym::{tcell::TCell, thread_key, tx::Ordering};

let x = TCell::new("hello");
let hello = thread_key::get().read(|tx| Ok(*x.borrow(tx, Ordering::Read)?));
assert_eq!(hello, "hello");

impl<T: Copy> TCell<T>[src]

#[must_use = "Calling `TCell::get` without using the result unnecessarily increases the chance of transaction failure"]
pub fn get<'tcell>(
    &'tcell self,
    tx: &impl Read<'tcell>,
    ordering: Ordering
) -> Result<T, Error>
[src]

Gets a copy of the contained value using the specified memory Ordering.

Statically requires that the TCell outlives the current transaction.

Errors

If another thread has written to this TCell during the current transaction, an error is returned.

Examples

use swym::{tcell::TCell, thread_key, tx::Ordering};

let x = TCell::new("hello");
let hello = thread_key::get().read(|tx| Ok(x.get(tx, Ordering::Read)?));
assert_eq!(hello, "hello");

impl<T: 'static + Send> TCell<T>[src]

pub fn set<'tcell>(
    &'tcell self,
    tx: &mut impl Write<'tcell>,
    value: T
) -> Result<(), SetError<T>>
[src]

Sets the contained value.

Statically requires that the TCell outlives the current transaction.

Errors

If another thread has written to this TCell during the current transaction, the value is not set, and an error is returned. It is typical to route this error back to ThreadKey::rw where the transaction will be retried, however, this is not required.

Examples

use swym::{tcell::TCell, thread_key};

let x = TCell::new("hello");
thread_key::get().rw(|tx| Ok(x.set(tx, "world")?));
assert_eq!(x.into_inner(), "world");

impl<T: 'static + Borrow + Clone + Send> TCell<T>[src]

pub fn replace<'tcell, 'tx>(
    &'tcell self,
    tx: &'tx mut impl RW<'tcell>,
    value: T
) -> Result<T, SetError<T>>
[src]

Trait Implementations

impl<T: Send + Sync> Sync for TCell<T>[src]

impl<T: Default> Default for TCell<T>[src]

impl<T: Send> Send for TCell<T>[src]

impl<T> From<T> for TCell<T>[src]

impl<T: Borrow + Debug> Debug for TCell<T>[src]

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

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

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

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

The type returned in the event of a conversion error.