pub struct StateTimeline<S: Storage = Heap> { /* private fields */ }Expand description
A discrete state over time, built from activity sets and checked against requirements.
The state equals the initial value from the start tick until the first set, and the value of
the latest set afterwards. Sets are kept sorted by tick, so a check costs
O(n + r log n) for n sets and r requirements and does not allocate unless it reports a
StateViolation::Conflict.
The storage S is storage::Heap by default; with
Fixed<N> the timeline holds at most N impacts and never
allocates. Heap timelines are created with new, timelines with any storage
with new_in.
Implementations§
Source§impl<S: Storage> StateTimeline<S>
impl<S: Storage> StateTimeline<S>
Sourcepub fn encoded_len(&self) -> usize
pub fn encoded_len(&self) -> usize
The number of bytes encode writes.
Sourcepub fn encode(&self, out: &mut [u8]) -> Result<usize, Error>
pub fn encode(&self, out: &mut [u8]) -> Result<usize, Error>
Writes the state timeline (start, initial value, budget, impacts in insertion order)
into out and returns the number of bytes written; see the codec
module for the format.
§Errors
Error::BufferTooSmall if out is shorter than encoded_len;
nothing is written then. Error::Overflow if the impact count exceeds u32.
Sourcepub fn decode_in(bytes: &[u8]) -> Result<Self, Error>
pub fn decode_in(bytes: &[u8]) -> Result<Self, Error>
Reads a state timeline written by encode, into storage S.
§Errors
Error::Malformed,Error::UnsupportedVersionorError::ChecksumMismatchas forLevelProfile::decode_in.- The errors of
StateTimeline::addfor the decoded content, including capacity errors ifSis smaller than the encoded timeline.
Source§impl StateTimeline
impl StateTimeline
Source§impl StateTimeline
impl StateTimeline
Sourcepub const fn new(start: Tick, initial: StateValue, budget: usize) -> Self
pub const fn new(start: Tick, initial: StateValue, budget: usize) -> Self
Creates a heap state timeline that holds initial from start on, with room for
budget impacts.
Source§impl<S: Storage> StateTimeline<S>
impl<S: Storage> StateTimeline<S>
Sourcepub const fn new_in(start: Tick, initial: StateValue, budget: usize) -> Self
pub const fn new_in(start: Tick, initial: StateValue, budget: usize) -> Self
Creates a state timeline with storage S that holds initial from start on, with
room for budget impacts.
As StateTimeline::new, for any Storage. With
Fixed<N>, adds beyond N impacts fail with
Error::CapacityExceeded even if the budget is larger.
Sourcepub const fn initial(&self) -> StateValue
pub const fn initial(&self) -> StateValue
The value from the start until the first set.
Sourcepub fn impacts(&self) -> &[(ActivityId, StateImpact)]
pub fn impacts(&self) -> &[(ActivityId, StateImpact)]
The impacts added so far, in insertion order.
Sourcepub fn add(
&mut self,
activity: ActivityId,
impact: StateImpact,
) -> Result<(), Error>
pub fn add( &mut self, activity: ActivityId, impact: StateImpact, ) -> Result<(), Error>
Adds an impact of activity.
§Errors
Error::BeforeStartif the impact refers to a tick before the start.Error::EmptyIntervalif a requirement hasend <= start.Error::BudgetExhaustedif the timeline already holdsbudgetimpacts.Error::OutOfMemoryif the impact lists cannot grow (heap storage).Error::CapacityExceededif a fixed impact list is full.
The timeline is unchanged on error.
Sourcepub fn remove(&mut self, activity: ActivityId) -> usize
pub fn remove(&mut self, activity: ActivityId) -> usize
Removes every impact of activity and returns how many there were. Never allocates.
Sourcepub fn value_at(&self, t: Tick) -> Result<Option<StateValue>, Error>
pub fn value_at(&self, t: Tick) -> Result<Option<StateValue>, Error>
The state value at tick t, or None if contradicting sets make it ambiguous there.
§Errors
Error::BeforeStart if t precedes the start.
Sourcepub fn values(&self) -> impl Iterator<Item = (Tick, Option<StateValue>)> + '_
pub fn values(&self) -> impl Iterator<Item = (Tick, Option<StateValue>)> + '_
The value changes over time for display and telemetry: (tick, value) pairs in time
order, starting with the initial value at the start tick.
A tick appears only if the value there differs from the one before; None marks a tick
where contradicting sets make the value ambiguous. Costs O(n log n) and never
allocates.
§Examples
use resource_timeline::{ActivityId, StateImpact, StateTimeline};
let mut mode = StateTimeline::new(0, 0, 8);
mode.add(ActivityId(1), StateImpact::Set { at: 5, value: 1 })?;
mode.add(ActivityId(2), StateImpact::Set { at: 9, value: 1 })?;
mode.add(ActivityId(3), StateImpact::Set { at: 12, value: 0 })?;
let changes: Vec<_> = mode.values().collect();
assert_eq!(changes, [(0, Some(0)), (5, Some(1)), (12, Some(0))]);Sourcepub fn first_violation(&self) -> Result<Option<StateViolation<S>>, Error>
pub fn first_violation(&self) -> Result<Option<StateViolation<S>>, Error>
Finds the earliest violation: a failed requirement or contradicting sets.
A conflict at tick c wins over mismatches at c or later, since the state is
undefined from there on. The result does not depend on insertion order.
§Errors
Error::OutOfMemory if the setter list of a conflict cannot be allocated (heap
storage). Fixed storage has room for it by construction.