Skip to main content

resource_timeline/
target.rs

1//! End-state targets: a minimum level required at one tick.
2
3use crate::storage::{Sealed, Slot};
4use crate::{Level, Tick};
5
6/// A minimum level the timeline must have at one tick, for example the battery reserve
7/// required at the end of a sol so the rover survives the night.
8///
9/// Unlike the [`Bounds`](crate::Bounds), which hold at every tick, a target holds at `at`
10/// only: activities after `at` may draw the level lower again. The level at `at` includes
11/// every impact at `at`. Targets take part in every check, so
12/// [`Timeline::first_violation`](crate::Timeline::first_violation), [`Plan::fits`](crate::Plan::fits)
13/// and [`Plan::earliest_start`](crate::Plan::earliest_start) reject schedules that miss one; a
14/// miss is reported as [`Bound::BelowTarget`](crate::Bound::BelowTarget).
15///
16/// # Examples
17///
18/// ```
19/// use resource_timeline::{ActivityId, Bound, Bounds, Impact, Target, Timeline};
20///
21/// // A sol lasts 100 ticks; 40 units must be left at its end.
22/// let mut battery = Timeline::depletable(0, 100, Bounds::new(10, 100)?, 8)?;
23/// battery.add_target(Target { at: 100, min: 40 })?;
24/// battery.add(ActivityId(1), Impact::Rate { start: 20, end: 90, per_tick: -1 })?;
25///
26/// let violation = battery.first_violation()?.expect("the end-of-sol reserve is missed");
27/// assert_eq!((violation.at, violation.level), (100, 30));
28/// assert_eq!(violation.bound, Bound::BelowTarget(40));
29/// # Ok::<(), resource_timeline::Error>(())
30/// ```
31#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33pub struct Target {
34    /// The tick at which the level is checked. Not before the timeline start.
35    pub at: Tick,
36    /// The lowest allowed level at `at`.
37    pub min: Level,
38}
39
40impl Target {
41    /// The sort key of stored targets: by tick, then strictest (highest minimum) first, so that
42    /// the first missed target at a tick is the strictest one.
43    pub(crate) const fn key(&self) -> (Tick, core::cmp::Reverse<Level>) {
44        (self.at, core::cmp::Reverse(self.min))
45    }
46}
47
48impl Sealed for Target {}
49
50impl Slot for Target {
51    const EMPTY: Self = Self { at: 0, min: 0 };
52}