pub struct TVar<T> { /* private fields */ }Expand description
A variable that can be used in a STM-Block
Implementations§
Source§impl<T> TVar<T>
 
impl<T> TVar<T>
Sourcepub fn read_atomic(&self) -> T
 
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.
Sourcepub fn read_ref_atomic(&self) -> Arc<dyn Any + Send + Sync>
 
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.
Sourcepub fn read(&self, transaction: &mut Transaction) -> StmClosureResult<T>
 
pub fn read(&self, transaction: &mut Transaction) -> StmClosureResult<T>
The normal way to access a var.
It is equivalent to transaction.read(&var), but more
convenient.
Sourcepub fn write(
    &self,
    transaction: &mut Transaction,
    value: T,
) -> StmClosureResult<()>
 
pub fn write( &self, transaction: &mut Transaction, value: T, ) -> StmClosureResult<()>
The normal way to write a var.
It is equivalent to transaction.write(&var, value), but more
convenient.
Sourcepub fn modify<F>(
    &self,
    transaction: &mut Transaction,
    f: F,
) -> StmClosureResult<()>where
    F: FnOnce(T) -> T,
 
pub fn modify<F>(
    &self,
    transaction: &mut Transaction,
    f: F,
) -> StmClosureResult<()>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);Sourcepub fn replace(
    &self,
    transaction: &mut Transaction,
    value: T,
) -> StmClosureResult<T>
 
pub fn replace( &self, transaction: &mut Transaction, value: T, ) -> StmClosureResult<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);Sourcepub fn ref_eq(this: &TVar<T>, other: &TVar<T>) -> bool
 
pub fn ref_eq(this: &TVar<T>, other: &TVar<T>) -> bool
Check if two TVars refer to the same position.
Sourcepub fn control_block(&self) -> &Arc<VarControlBlock>
 
pub fn control_block(&self) -> &Arc<VarControlBlock>
Access the control block of the var.
Internal use only!
Trait Implementations§
Source§impl<T> Debug for TVar<T>
Debug output a struct.
 
impl<T> Debug for TVar<T>
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.