fast_stm

Struct TVar

Source
pub struct TVar<T> { /* private fields */ }
Expand description

A variable that can be used in a STM-Block

Implementations§

Source§

impl<T> TVar<T>
where T: Any + Sync + Send + Clone,

Source

pub fn new(val: T) -> TVar<T>

Create a new TVar.

Source

pub fn read_atomic(&self) -> T

read_atomic reads a value atomically, without starting a transaction.

It is semantically equivalent to


let var = TVar::new(0);
atomically(|trans| var.read(trans));

but more efficient.

read_atomic returns a clone of the value.

Source

pub fn read_ref_atomic(&self) -> Arc<dyn Any + Send + Sync>

Read a value atomically but return a reference.

This is mostly used internally, but can be useful in some cases, because read_atomic clones the inner value, which may be expensive.

Source

pub fn read(&self, transaction: &mut Transaction) -> StmResult<T>

The normal way to access a var.

It is equivalent to transaction.read(&var), but more convenient.

Source

pub fn write(&self, transaction: &mut Transaction, value: T) -> StmResult<()>

The normal way to write a var.

It is equivalent to transaction.write(&var, value), but more convenient.

Source

pub fn modify<F>(&self, transaction: &mut Transaction, f: F) -> StmResult<()>
where F: FnOnce(T) -> T,

Modify the content of a TVar with the function f.



let var = TVar::new(21);
atomically(|trans|
    var.modify(trans, |x| x*2)
);

assert_eq!(var.read_atomic(), 42);
Source

pub fn replace(&self, transaction: &mut Transaction, value: T) -> StmResult<T>

Replaces the value of a TVar with a new one, returning the old one.


let var = TVar::new(0);
let x = atomically(|trans|
    var.replace(trans, 42)
);

assert_eq!(x, 0);
assert_eq!(var.read_atomic(), 42);
Source

pub fn ref_eq(this: &TVar<T>, other: &TVar<T>) -> bool

Check if two TVars refer to the same position.

Source

pub fn control_block(&self) -> &Arc<VarControlBlock>

Access the control block of the var.

Internal use only!

Trait Implementations§

Source§

impl<T: Clone> Clone for TVar<T>

Source§

fn clone(&self) -> TVar<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for TVar<T>
where T: Any + Sync + Send + Clone + Debug,

Debug output a struct.

Note that this function does not print the state atomically. If another thread modifies the datastructure at the same time, it may print an inconsistent state. If you need an accurate view, that reflects current thread-local state, you can implement it easily yourself with atomically.

Running atomically inside a running transaction panics. Therefore fmt uses prints the state.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for TVar<T>

§

impl<T> !RefUnwindSafe for TVar<T>

§

impl<T> Send for TVar<T>
where T: Send,

§

impl<T> Sync for TVar<T>
where T: Sync,

§

impl<T> Unpin for TVar<T>
where T: Unpin,

§

impl<T> !UnwindSafe for TVar<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.