pub struct WorkerState<T: 'static> { /* private fields */ }Expand description
Per-worker state storage.
This provides thread-local storage for arbitrary state that needs to be accessed on the hot path without Arc cloning overhead.
§Use Cases
- Database connection pools (one per worker)
- Caches (per-worker to avoid contention)
- Metrics collectors
- Random number generators
- Pre-allocated buffers
§Example
ⓘ
use armature_core::worker::{WorkerState, init_worker_state};
// Define state
struct MyState {
counter: u64,
buffer: Vec<u8>,
}
// Initialize once per worker
init_worker_state(MyState {
counter: 0,
buffer: Vec::with_capacity(4096),
});
// Access without Arc cloning
WorkerState::<MyState>::with_mut(|state| {
state.counter += 1;
});Implementations§
Source§impl<T: 'static + Send> WorkerState<T>
impl<T: 'static + Send> WorkerState<T>
Sourcepub fn init(value: T)
pub fn init(value: T)
Initialize state for this worker.
Call once per worker thread during startup.
Sourcepub fn with<F, R>(f: F) -> R
pub fn with<F, R>(f: F) -> R
Access state immutably.
§Panics
Panics if state was not initialized. Use try_with for non-panicking.
Sourcepub fn try_with<F, R>(f: F) -> Option<R>
pub fn try_with<F, R>(f: F) -> Option<R>
Try to access state immutably.
Returns None if state was not initialized.
Sourcepub fn try_with_mut<F, R>(f: F) -> Option<R>
pub fn try_with_mut<F, R>(f: F) -> Option<R>
Try to access state mutably.
Returns None if state was not initialized.
Sourcepub fn is_initialized() -> bool
pub fn is_initialized() -> bool
Check if state is initialized.
Auto Trait Implementations§
impl<T> Freeze for WorkerState<T>
impl<T> RefUnwindSafe for WorkerState<T>where
T: RefUnwindSafe,
impl<T> Send for WorkerState<T>where
T: Send,
impl<T> Sync for WorkerState<T>where
T: Sync,
impl<T> Unpin for WorkerState<T>where
T: Unpin,
impl<T> UnwindSafe for WorkerState<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
Source§impl<T> Injectable for T
impl<T> Injectable for T
Source§fn type_id_of() -> TypeIdwhere
Self: Sized,
fn type_id_of() -> TypeIdwhere
Self: Sized,
Returns the TypeId of this type (for internal use)
Source§fn type_name_of() -> &'static strwhere
Self: Sized,
fn type_name_of() -> &'static strwhere
Self: Sized,
Returns the type name for debugging