Skip to main content

Step

Struct Step 

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

Source

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

pub fn range(&self) -> &Range<T>

Returns a reference to the range covered by this step.

Source

pub fn value(&self) -> &V

Returns a reference to the value associated with this step.

Source

pub fn start(&self) -> &T

Returns a reference to the inclusive start bound of the range.

Source

pub fn end(&self) -> &T

Returns a reference to the exclusive end bound of the range.

Source

pub fn contains(&self, time: &T) -> bool

Returns true if time falls within the range of this step.

Equivalent to self.range.contains(time).

Source

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

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.

Source

pub fn cmp_to_time(&self, time: &T) -> Ordering

Returns how this step’s range relates to a given time value.

Useful for efficient searching (e.g., with binary_search_by).

Trait Implementations§

Source§

impl<T: Clone, V: Clone> Clone for Step<T, V>

Source§

fn clone(&self) -> Step<T, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, V: Debug> Debug for Step<T, V>

Source§

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

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

impl<T: PartialEq, V: PartialEq> PartialEq for Step<T, V>

Source§

fn eq(&self, other: &Step<T, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Debug + Ord, V> TryFrom<(Range<T>, V)> for Step<T, V>

Source§

type Error = EmptyRangeError<T>

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

fn try_from((range, value): (Range<T>, V)) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: Eq, V: Eq> Eq for Step<T, V>

Source§

impl<T, V> StructuralPartialEq for Step<T, V>

Auto Trait Implementations§

§

impl<T, V> Freeze for Step<T, V>
where V: Freeze, T: Freeze,

§

impl<T, V> RefUnwindSafe for Step<T, V>

§

impl<T, V> Send for Step<T, V>
where V: Send, T: Send,

§

impl<T, V> Sync for Step<T, V>
where V: Sync, T: Sync,

§

impl<T, V> Unpin for Step<T, V>
where V: Unpin, T: Unpin,

§

impl<T, V> UnsafeUnpin for Step<T, V>
where V: UnsafeUnpin, T: UnsafeUnpin,

§

impl<T, V> UnwindSafe for Step<T, V>
where V: UnwindSafe, T: 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> 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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.