pub struct SystemState { /* private fields */ }Expand description
A heterogeneous collection of payloads describing one system time point.
Fields are declared by a JSON-derived StateSpec. Values are addressed
by those field names but stored in compact optional slots. The type-erased
representation remains private; callers always insert, borrow, mutate, and
extract concrete Rust types.
Implementations§
Source§impl SystemState
impl SystemState
Sourcepub fn empty(&self, time: TimePoint) -> Self
pub fn empty(&self, time: TimePoint) -> Self
Creates another empty state with the same shared specification.
No payload is cloned. Only the immutable specification handle is
cloned, which increments an internal Arc reference count.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of fields declared by the state specification.
This count is structural and includes empty payload slots.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Reports whether the state specification declares no fields.
This is consistent with SystemState::len. To test whether a
non-empty layout currently carries no payloads, use
SystemState::is_blank.
Sourcepub fn fields(&self) -> &[FieldSpec]
pub fn fields(&self) -> &[FieldSpec]
Returns field specifications in deterministic template order.
Sourcepub fn has(&self, key: &str) -> Result<bool, StateError>
pub fn has(&self, key: &str) -> Result<bool, StateError>
Reports whether a declared field currently contains a payload.
§Errors
Returns StateError::UnknownField when key was not declared by the
JSON template.
Sourcepub fn is<T>(&self, key: &str) -> Result<bool, StateError>where
T: Any,
pub fn is<T>(&self, key: &str) -> Result<bool, StateError>where
T: Any,
Reports whether a populated field contains the exact Rust type T.
An empty declared field returns false.
§Errors
Returns StateError::UnknownField when key was not declared by the
JSON template.
Sourcepub fn set<T>(&mut self, key: &str, payload: T) -> Result<(), StateError>
pub fn set<T>(&mut self, key: &str, payload: T) -> Result<(), StateError>
Sets or replaces the payload in a declared field.
payload moves into the state and is never cloned. If the slot was
already populated, its previous value is dropped. Call take
first when the previous payload must be retained.
§Errors
Returns StateError::UnknownField when key was not declared by the
JSON template. The payload is dropped with the returned error because
this minimal API does not expose the internal erased-value wrapper.
Sourcepub fn get<T>(&self, key: &str) -> Result<&T, StateError>where
T: Any,
pub fn get<T>(&self, key: &str) -> Result<&T, StateError>where
T: Any,
Borrows a populated field as the exact Rust type T.
§Errors
Returns StateError::UnknownField for an undeclared key,
StateError::MissingValue for an empty slot, or
StateError::TypeMismatch when the stored concrete type differs from
T.
Sourcepub fn get_mut<T>(&mut self, key: &str) -> Result<&mut T, StateError>where
T: Any,
pub fn get_mut<T>(&mut self, key: &str) -> Result<&mut T, StateError>where
T: Any,
Mutably borrows a populated field as the exact Rust type T.
Mutation occurs in place and does not clone the payload.
§Errors
Returns StateError::UnknownField for an undeclared key,
StateError::MissingValue for an empty slot, or
StateError::TypeMismatch when the stored concrete type differs from
T.
Sourcepub fn take<T>(&mut self, key: &str) -> Result<T, StateError>
pub fn take<T>(&mut self, key: &str) -> Result<T, StateError>
Removes and returns the payload from a declared field.
A successful call moves the original concrete T out of its internal
box and leaves the field slot empty. It does not invoke Clone. If T
does not match, the original erased value is restored before the error
is returned.
§Errors
Returns StateError::UnknownField for an undeclared key,
StateError::MissingValue for an empty slot, or
StateError::TypeMismatch when the stored concrete type differs from
T.
Sourcepub fn clear(&mut self, key: &str) -> Result<bool, StateError>
pub fn clear(&mut self, key: &str) -> Result<bool, StateError>
Drops the payload stored in one declared field.
Returns true when a payload was present and dropped, or false when
the declared slot was already empty.
§Errors
Returns StateError::UnknownField when key was not declared by the
JSON template.