Skip to main content

StateTimeline

Struct StateTimeline 

Source
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>

Source

pub fn encoded_len(&self) -> usize

The number of bytes encode writes.

Source

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.

Source

pub fn decode_in(bytes: &[u8]) -> Result<Self, Error>

Reads a state timeline written by encode, into storage S.

§Errors
Source§

impl StateTimeline

Source

pub fn decode(bytes: &[u8]) -> Result<Self, Error>

decode_in into heap storage.

§Errors

As for decode_in.

Source§

impl StateTimeline

Source

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>

Source

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.

Source

pub const fn start(&self) -> Tick

The first tick of the timeline.

Source

pub const fn initial(&self) -> StateValue

The value from the start until the first set.

Source

pub const fn budget(&self) -> usize

The maximum number of impacts the timeline accepts.

Source

pub fn impacts(&self) -> &[(ActivityId, StateImpact)]

The impacts added so far, in insertion order.

Source

pub fn add( &mut self, activity: ActivityId, impact: StateImpact, ) -> Result<(), Error>

Adds an impact of activity.

§Errors

The timeline is unchanged on error.

Source

pub fn remove(&mut self, activity: ActivityId) -> usize

Removes every impact of activity and returns how many there were. Never allocates.

Source

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.

Source

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))]);
Source

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.

Trait Implementations§

Source§

impl<S: Clone + Storage> Clone for StateTimeline<S>
where S::List<(ActivityId, StateImpact)>: Clone, S::List<SetPoint>: Clone, S::List<Requirement>: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + Storage> Debug for StateTimeline<S>
where S::List<(ActivityId, StateImpact)>: Debug, S::List<SetPoint>: Debug, S::List<Requirement>: Debug,

Source§

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

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

impl<'de, S: Storage> Deserialize<'de> for StateTimeline<S>

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<S: Eq + Storage> Eq for StateTimeline<S>
where S::List<(ActivityId, StateImpact)>: Eq, S::List<SetPoint>: Eq, S::List<Requirement>: Eq,

Source§

impl<S: Hash + Storage> Hash for StateTimeline<S>
where S::List<(ActivityId, StateImpact)>: Hash, S::List<SetPoint>: Hash, S::List<Requirement>: Hash,

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<S: PartialEq + Storage> PartialEq for StateTimeline<S>
where S::List<(ActivityId, StateImpact)>: PartialEq, S::List<SetPoint>: PartialEq, S::List<Requirement>: PartialEq,

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<S: Storage> Serialize for StateTimeline<S>

Source§

fn serialize<Z: Serializer>(&self, serializer: Z) -> Result<Z::Ok, Z::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<S: Storage> Slot for StateTimeline<S>

Source§

const EMPTY: Self

The filler of unused slots. Never observable through a container.
Source§

impl<S: PartialEq + Storage> StructuralPartialEq for StateTimeline<S>
where S::List<(ActivityId, StateImpact)>: PartialEq, S::List<SetPoint>: PartialEq, S::List<Requirement>: PartialEq,

Auto Trait Implementations§

§

impl<S> Forget for StateTimeline<S>

§

impl<S> Freeze for StateTimeline<S>
where <S as Storage>::List<(ActivityId, StateImpact)>: Freeze, <S as Storage>::List<SetPoint>: Freeze, <S as Storage>::List<Requirement>: Freeze,

§

impl<S> Move for StateTimeline<S>

§

impl<S> RefUnwindSafe for StateTimeline<S>
where <S as Storage>::List<(ActivityId, StateImpact)>: RefUnwindSafe, <S as Storage>::List<SetPoint>: RefUnwindSafe, <S as Storage>::List<Requirement>: RefUnwindSafe,

§

impl<S> Send for StateTimeline<S>
where <S as Storage>::List<(ActivityId, StateImpact)>: Send, <S as Storage>::List<SetPoint>: Send, <S as Storage>::List<Requirement>: Send,

§

impl<S> Sync for StateTimeline<S>
where <S as Storage>::List<(ActivityId, StateImpact)>: Sync, <S as Storage>::List<SetPoint>: Sync, <S as Storage>::List<Requirement>: Sync,

§

impl<S> Unpin for StateTimeline<S>
where <S as Storage>::List<(ActivityId, StateImpact)>: Unpin, <S as Storage>::List<SetPoint>: Unpin, <S as Storage>::List<Requirement>: Unpin,

§

impl<S> UnsafeUnpin for StateTimeline<S>
where <S as Storage>::List<(ActivityId, StateImpact)>: UnsafeUnpin, <S as Storage>::List<SetPoint>: UnsafeUnpin, <S as Storage>::List<Requirement>: UnsafeUnpin,

§

impl<S> UnwindSafe for StateTimeline<S>
where <S as Storage>::List<(ActivityId, StateImpact)>: UnwindSafe, <S as Storage>::List<SetPoint>: UnwindSafe, <S as Storage>::List<Requirement>: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.