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 [StepSchedule],
which assign values to non-overlapping intervals.
§Examples
use twine_components::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_components::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_components::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).
Trait Implementations§
impl<T: Eq, V: Eq> Eq for Step<T, V>
impl<T, V> StructuralPartialEq for Step<T, V>
Auto Trait Implementations§
impl<T, V> Freeze for Step<T, V>
impl<T, V> RefUnwindSafe for Step<T, V>where
V: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, V> Send for Step<T, V>
impl<T, V> Sync for Step<T, V>
impl<T, V> Unpin for Step<T, V>
impl<T, V> UnwindSafe for Step<T, V>where
V: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.