pub struct Step<T, V> { /* private fields */ }Expand description
Associates a value with a non-empty, half-open time range.
A Step pairs a value with the range [start, end), where start < end.
The type T must implement Ord and typically represents time.
Steps are used as building blocks for schedules like super::StepSchedule,
which assign values to non-overlapping intervals.
§Examples
use twine_models::support::schedule::step_schedule::Step;
let step = Step::new(0..10, "active").unwrap();
assert!(step.contains(&5));
assert_eq!(step.value(), &"active");
assert!(Step::new(2..2, "empty").is_err());Implementations§
Source§impl<T: Debug + Ord, V> Step<T, V>
impl<T: Debug + Ord, V> Step<T, V>
Sourcepub fn new(range: Range<T>, value: V) -> Result<Self, EmptyRangeError<T>>
pub fn new(range: Range<T>, value: V) -> Result<Self, EmptyRangeError<T>>
Creates a new Step with the given range and value.
§Errors
Returns an EmptyRangeError if the provided range is empty.
§Examples
use twine_models::support::schedule::step_schedule::Step;
let step = Step::new(0..10, 42.0).unwrap();
assert_eq!(step.start(), &0);
assert_eq!(step.end(), &10);
assert_eq!(step.value(), &42.0);
assert!(Step::new(5..1, "invalid range").is_err());Sourcepub fn contains(&self, time: &T) -> bool
pub fn contains(&self, time: &T) -> bool
Returns true if time falls within the range of this step.
Equivalent to self.range.contains(time).
Sourcepub fn overlaps(&self, other: &Self) -> bool
pub fn overlaps(&self, other: &Self) -> bool
Returns true if this step’s range overlaps with other’s range.
Two steps overlap if their ranges share any values.
§Examples
use twine_models::support::schedule::step_schedule::Step;
let a = Step::new(0..5, "a").unwrap();
let b = Step::new(4..8, "b").unwrap();
let c = Step::new(8..10, "c").unwrap();
assert!(a.overlaps(&b));
assert!(!b.overlaps(&c));Sourcepub fn value_at(&self, time: &T) -> Option<&V>
pub fn value_at(&self, time: &T) -> Option<&V>
Returns a reference to the value if time is within this step’s range.
Returns None if time is outside the range.
Sourcepub fn cmp_to_time(&self, time: &T) -> Ordering
pub fn cmp_to_time(&self, time: &T) -> Ordering
Returns how this step’s range relates to a given time value.
Ordering::Lessif the step ends at or beforetimeOrdering::Greaterif the step starts aftertimeOrdering::Equaliftimeis within the step’s range
Useful for efficient searching (e.g., with binary_search_by).