State

Struct State 

Source
pub struct State<T = i32>
where T: ToString,
{ /* private fields */ }
Expand description

Represents a state container for a value that tracks its current, previous, and before-previous states.

This generic struct allows tracking changes to a value over time, with the ability to store the current value, its previous value, and the value before the previous one. The type T must implement the ToString trait.

§Type Parameters

  • T - The type of the value being tracked, defaulting to i32 if not specified.

§Fields

  • before_previous - The value that existed before the previous value.
  • previous - The previous value of the state.
  • current - The current value of the state.
  • transacting - A flag indicating whether a transaction is in progress (to be removed).

/// # Thread Safety

This type is intentionally not thread-safe and will panic if accessed from multiple threads. For multi-threaded applications, use thread-local storage to maintain separate instances per thread.

use std::thread;
use std::cell::RefCell;
use transactional::prelude::*;

thread_local! {
    static LOCAL_STATE: RefCell<State<i32>> = RefCell::new(State::new(0));
}

// Safe usage: each thread accesses its own instance
thread::spawn(|| {
    LOCAL_STATE.with(|state| {
        let mut state = state.borrow_mut();
        // Use state safely within this thread
    });
});

Trait Implementations§

Source§

impl<T> Debug for State<T>
where T: ToString + Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for State<T>
where T: ToString,

Source§

fn default() -> Self

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

impl<T> StateBehavior for State<T>
where T: ToString,

Source§

type Item = T

Source§

fn get(&self) -> Option<&T>

Source§

fn get_previous(&self) -> Option<&T>

Source§

fn transact( &mut self, value: Option<Self::Item>, ) -> Transaction<'_, Self::Item, Transacting>

Source§

impl<T> StateFactory for State<T>
where T: ToString,

Source§

type Item = T

Source§

fn new(value: Self::Item) -> State<Self::Item>

Source§

impl<T> ThreadSafetyChecker for State<T>
where T: ToString,

Auto Trait Implementations§

§

impl<T> Freeze for State<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for State<T>
where T: RefUnwindSafe,

§

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

§

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

§

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

§

impl<T> UnwindSafe for State<T>
where T: UnwindSafe,

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> 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, 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.