Skip to main content

resource_timeline/
window.rs

1//! Start windows of activities whose start time is not fixed yet.
2
3use crate::{Error, Tick};
4
5/// The inclusive range `[earliest, latest]` of allowed start ticks of an activity.
6///
7/// This is the shape of a time point's window in a simple temporal network: in
8/// `temporal-control`, `MinimalNetwork::lower(origin, tp)` and `upper(origin, tp)`, or
9/// `Dispatcher::window(tp)` during execution. Unbounded sides there must be clamped to a
10/// mission horizon before they become a `Window`.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub struct Window {
13    /// The earliest allowed start. Never greater than `latest`.
14    earliest: Tick,
15    /// The latest allowed start.
16    latest: Tick,
17}
18
19impl Window {
20    /// Creates the window `[earliest, latest]`.
21    ///
22    /// # Errors
23    ///
24    /// [`Error::InvalidWindow`] if `earliest > latest`.
25    pub const fn new(earliest: Tick, latest: Tick) -> Result<Self, Error> {
26        if earliest > latest {
27            return Err(Error::InvalidWindow { earliest, latest });
28        }
29
30        Ok(Self { earliest, latest })
31    }
32
33    /// The window that allows only `start`, for an activity with a fixed start.
34    #[must_use]
35    pub const fn at(start: Tick) -> Self {
36        Self {
37            earliest: start,
38            latest: start,
39        }
40    }
41
42    /// The earliest allowed start.
43    #[must_use]
44    pub const fn earliest(&self) -> Tick {
45        self.earliest
46    }
47
48    /// The latest allowed start.
49    #[must_use]
50    pub const fn latest(&self) -> Tick {
51        self.latest
52    }
53
54    /// Whether `t` is an allowed start.
55    #[must_use]
56    pub const fn contains(&self, t: Tick) -> bool {
57        self.earliest <= t && t <= self.latest
58    }
59}