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 toi32if 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> StateBehavior for State<T>where
T: ToString,
impl<T> StateBehavior for State<T>where
T: ToString,
Source§impl<T> StateFactory for State<T>where
T: ToString,
impl<T> StateFactory for State<T>where
T: ToString,
Source§impl<T> ThreadSafetyChecker for State<T>where
T: ToString,
impl<T> ThreadSafetyChecker for State<T>where
T: ToString,
fn get_owner_thread_id(&self) -> ThreadId
fn ensure_thread_safety(&self)
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more