Struct TimeWindow

Source
pub struct TimeWindow { /* private fields */ }
Expand description

An interval or range of time: [start,end). Debug-asserts ensure that start <= end. If compiled in release mode, the invariant of start <= end is maintained, by correcting invalid use of the API (and setting end to start).

Implementations§

Source§

impl TimeWindow

Source

pub fn new(start: Time, end: Time) -> Self

Constructs a new TimeWindow. debug_asserts that start < end. Sets end to start in release mode if start > end.

Source

pub fn new_checked(start: Time, end: Time) -> Result<Self, TimeWindowError>

Constructs a new TimeWindow. Validates that start <= end and returns an error if not.

§Examples
assert!(TimeWindow::new_checked(Time::hours(1), Time::hours(2)).is_ok());
assert_eq!(
    Err(TimeWindowError::StartAfterEnd),
    TimeWindow::new_checked(Time::hours(2), Time::hours(1))
);
Source

pub fn epoch_to(end: Time) -> Self

Returns TimeWindow with range [Time::EPOCH, end)

Source

pub fn from_minutes(a: i64, b: i64) -> Self

Source

pub fn from_seconds(a: i64, b: i64) -> Self

Source

pub fn from_length_starting_at(length: Duration, start: Time) -> Self

Creates time window from start time and length.

Negative lengths are treated as Duration::ZERO.

§Examples
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    TimeWindow::from_length_starting_at(Duration::seconds(2), Time::seconds(1))
);
assert_eq!(
    TimeWindow::from_seconds(1, 1),
    TimeWindow::from_length_starting_at(Duration::seconds(-2), Time::seconds(1))
);
Source

pub fn from_length_ending_at(length: Duration, end: Time) -> Self

Creates time window from length and end time.

Negative lengths are treated as Duration::ZERO.

§Examples
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    TimeWindow::from_length_ending_at(Duration::seconds(2), Time::seconds(3))
);
assert_eq!(
    TimeWindow::from_seconds(3, 3),
    TimeWindow::from_length_ending_at(Duration::seconds(-2), Time::seconds(3))
);
Source

pub const fn instant(time: Time) -> Self

Source

pub const fn widest() -> Self

Source

pub fn instant_seconds(seconds: i64) -> Self

Source

pub const fn start(&self) -> Time

Source

pub const fn end(&self) -> Time

Source

pub fn length(&self) -> Duration

Source

pub fn with_start(&self, new_start: Time) -> Self

Creates a new TimeWindow with start set to new_start. If new_start is greater than or equal to end the start will be set equal to end.

Source

pub fn with_end(&self, new_end: Time) -> Self

Creates a new TimeWindow with end set to new_end. If new_end is smaller or equal to start, the end will be set to start.

Source

pub fn prepone_start_to(&self, new_start: Time) -> Self

Creates a new TimeWindow with the start preponed to the given value. If new_start isn’t earlier than the current time window start, a copy of self is returned.

§Examples
let x = TimeWindow::from_seconds(4, 5);
assert_eq!(
    TimeWindow::from_seconds(3, 5),
    x.prepone_start_to(Time::seconds(3))
);
assert_eq!(
    TimeWindow::from_seconds(4, 5),
    x.prepone_start_to(Time::seconds(6))
);
Source

pub fn prepone_start_by(&self, duration: Duration) -> Self

Creates a new TimeWindow with the start preponed by the given duration.

Negative durations are treated as Duration::ZERO.

§Examples
let tw = TimeWindow::from_seconds(8, 9);
assert_eq!(
    TimeWindow::from_seconds(5, 9),
    tw.prepone_start_by(Duration::seconds(3))
);
assert_eq!(
    TimeWindow::from_seconds(8, 9),
    tw.prepone_start_by(Duration::seconds(-3))
);
Source

pub fn prepone_start_extend_to(&self, new_length: Duration) -> Self

Creates a new TimeWindow with the start preponed so that the new time window length matches the given value.

Returns a copy of self if the new length is smaller than Self::length().

§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    tw.prepone_start_extend_to(Duration::seconds(-1))
);
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    tw.prepone_start_extend_to(Duration::seconds(0))
);
assert_eq!(
    TimeWindow::from_seconds(-2, 3),
    tw.prepone_start_extend_to(Duration::seconds(5))
);
Source

pub fn postpone_start_to(&self, new_start: Time) -> Self

Creates a new TimeWindow with the start postponed to the given value.

Returns a copy of self when the given value isn’t later than the current time window start. Will never postpone the start past the end of the time window.

§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    tw.postpone_start_to(Time::EPOCH)
);
assert_eq!(
    TimeWindow::from_seconds(2, 3),
    tw.postpone_start_to(Time::seconds(2))
);
assert_eq!(
    TimeWindow::from_seconds(3, 3),
    tw.postpone_start_to(Time::seconds(3))
);
Source

pub fn postpone_start_by(&self, duration: Duration) -> Self

Creates a new TimeWindow with the start postponed by the given duration.

Negative durations are treated as Duration::ZERO. Will not postpone start further than end.

§Examples
let tw = TimeWindow::from_seconds(1, 5);
assert_eq!(
    TimeWindow::from_seconds(4, 5),
    tw.postpone_start_by(Duration::seconds(3))
);
assert_eq!(
    TimeWindow::from_seconds(5, 5),
    tw.postpone_start_by(Duration::seconds(30))
);
assert_eq!(
    TimeWindow::from_seconds(1, 5),
    tw.postpone_start_by(Duration::seconds(-3))
);
Source

pub fn postpone_start_shrink_to(&self, new_length: Duration) -> Self

Creates a new TimeWindow with the start postponed so that the new time window length matches the given value.

Returns a copy of self if the new length is smaller than the current one. Negative length will set the resulting time window length to zero.

§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
    TimeWindow::from_seconds(3, 3),
    tw.postpone_start_shrink_to(Duration::seconds(-1))
);
assert_eq!(
    TimeWindow::from_seconds(3, 3),
    tw.postpone_start_shrink_to(Duration::seconds(0))
);
assert_eq!(
    TimeWindow::from_seconds(2, 3),
    tw.postpone_start_shrink_to(Duration::seconds(1))
);
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    tw.postpone_start_shrink_to(Duration::seconds(5))
);
Source

pub fn prepone_end_to(&self, new_end: Time) -> Self

Creates a new TimeWindow with the end preponed to the given value.

Returns a copy of self when the given value isn’t earlier than the current time window end. Will never prepone the end more than to the start of the time window.

§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    tw.prepone_end_to(Time::seconds(4))
);
assert_eq!(
    TimeWindow::from_seconds(1, 2),
    tw.prepone_end_to(Time::seconds(2))
);
assert_eq!(
    TimeWindow::from_seconds(1, 1),
    tw.prepone_end_to(Time::EPOCH)
);
Source

pub fn prepone_end_by(&self, duration: Duration) -> Self

Creates a new TimeWindow with the end preponed by the given duration.

Negative durations are treated as Duration::ZERO. Will not prepone end before end.

§Examples
let tw = TimeWindow::from_seconds(4, 9);
assert_eq!(
    TimeWindow::from_seconds(4, 6),
    tw.prepone_end_by(Duration::seconds(3))
);
assert_eq!(
    TimeWindow::from_seconds(4, 4),
    tw.prepone_end_by(Duration::seconds(30))
);
assert_eq!(
    TimeWindow::from_seconds(4, 9),
    tw.prepone_end_by(Duration::seconds(-3))
);
Source

pub fn prepone_end_shrink_to(&self, new_length: Duration) -> Self

Creates a new TimeWindow with the end preponed so that the new time window length matches the given value.

Returns a copy of self if the new length is smaller than the current one. Negative length will set the resulting time window length to zero.

§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
    TimeWindow::from_seconds(1, 1),
    tw.prepone_end_shrink_to(Duration::seconds(-1))
);
assert_eq!(
    TimeWindow::from_seconds(1, 1),
    tw.prepone_end_shrink_to(Duration::seconds(0))
);
assert_eq!(
    TimeWindow::from_seconds(1, 2),
    tw.prepone_end_shrink_to(Duration::seconds(1))
);
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    tw.prepone_end_shrink_to(Duration::seconds(5))
);
Source

pub fn postpone_end_to(&self, new_end: Time) -> Self

Creates a new TimeWindow with the end postponed to the given value. If new_end isn’t later than the current time window end, a copy of self is returned.

§Examples
let x = TimeWindow::from_seconds(1, 2);
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    x.postpone_end_to(Time::seconds(3))
);
assert_eq!(
    TimeWindow::from_seconds(1, 2),
    x.postpone_end_to(Time::EPOCH)
);
Source

pub fn postpone_end_by(&self, duration: Duration) -> Self

Creates a new TimeWindow with the end postponed by the given duration.

Negative durations are treated as Duration::ZERO.

§Examples
let tw = TimeWindow::from_seconds(1, 2);
assert_eq!(
    TimeWindow::from_seconds(1, 5),
    tw.postpone_end_by(Duration::seconds(3))
);
assert_eq!(
    TimeWindow::from_seconds(1, 2),
    tw.postpone_end_by(Duration::seconds(-3))
);
Source

pub fn postpone_end_extend_to(&self, new_length: Duration) -> Self

Creates a new TimeWindow with the end postponed so that the new time window length matches the given value.

Returns a copy of self if the new length is smaller than Self::length().

§Examples
let tw = TimeWindow::from_seconds(1, 3);
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    tw.postpone_end_extend_to(Duration::seconds(-1))
);
assert_eq!(
    TimeWindow::from_seconds(1, 3),
    tw.postpone_end_extend_to(Duration::seconds(0))
);
assert_eq!(
    TimeWindow::from_seconds(1, 6),
    tw.postpone_end_extend_to(Duration::seconds(5))
);
Source

pub fn contains(&self, that: Time) -> bool

Returns true if this time window contains the given time.

§Examples
let mut x = TimeWindow::from_seconds(5, 10);
assert!(!x.contains(Time::seconds(4)));
assert!(x.contains(Time::seconds(5)));
assert!(x.contains(Time::seconds(7)));
assert!(x.contains(Time::seconds(10)));
assert!(!x.contains(Time::seconds(11)));
Source

pub fn overlaps(&self, that: &TimeWindow) -> bool

Returns true if this time window overlaps with another one

§Examples
let mut x = TimeWindow::from_seconds(5, 10);
assert!(x.overlaps(&TimeWindow::from_seconds(5, 10)));
assert!(x.overlaps(&TimeWindow::from_seconds(3, 12)));
assert!(x.overlaps(&TimeWindow::from_seconds(6, 9)));
assert!(x.overlaps(&TimeWindow::from_seconds(6, 12)));
assert!(x.overlaps(&TimeWindow::from_seconds(3, 9)));
assert!(!x.overlaps(&TimeWindow::from_seconds(1, 4)));
assert!(!x.overlaps(&TimeWindow::from_seconds(1, 5)));
assert!(!x.overlaps(&TimeWindow::from_seconds(10, 15)));
assert!(!x.overlaps(&TimeWindow::from_seconds(11, 15)));
Source

pub fn intersect(&self, that: &TimeWindow) -> Option<TimeWindow>

Returns time window that is an intersection between this time window and another one. Returns None if time windows don’t overlap.

§Examples
let x = TimeWindow::from_seconds(5, 10);
assert_eq!(
    Some(TimeWindow::from_seconds(5, 10)),
    x.intersect(&TimeWindow::from_seconds(5, 10)),
    "time windows are equal"
);
assert_eq!(
    Some(TimeWindow::from_seconds(5, 10)),
    x.intersect(&TimeWindow::from_seconds(3, 12)),
    "that contains x"
);
assert_eq!(
    Some(TimeWindow::from_seconds(6, 9)),
    x.intersect(&TimeWindow::from_seconds(6, 9)),
    "x contains that"
);
assert_eq!(
    Some(TimeWindow::from_seconds(6, 10)),
    x.intersect(&TimeWindow::from_seconds(6, 12))
);
assert_eq!(
    Some(TimeWindow::from_seconds(5, 9)),
    x.intersect(&TimeWindow::from_seconds(3, 9))
);
assert_eq!(
    None,
    x.intersect(&TimeWindow::from_seconds(1, 4)),
    "that is before x"
);
assert_eq!(
    Some(TimeWindow::from_seconds(5, 5)),
    x.intersect(&TimeWindow::from_seconds(1, 5)),
    "single-point intersection"
);
assert_eq!(
    Some(TimeWindow::from_seconds(10, 10)),
    x.intersect(&TimeWindow::from_seconds(10, 15)),
    "single-point intersection"
);
assert_eq!(
    None,
    x.intersect(&TimeWindow::from_seconds(11, 15)),
    "that is after x"
);
Source

pub fn shift(&mut self, duration: Duration)

Shifts this time window by duration into the future. Affects both start and end equally, leaving the length untouched.

§Examples
let mut tw = TimeWindow::new(Time::EPOCH, Time::minutes(15));
// shift to the future
tw.shift(Duration::minutes(30));
assert_eq!(TimeWindow::new(Time::minutes(30), Time::minutes(45)), tw);
// shift into the past
tw.shift(-Duration::minutes(15));
assert_eq!(TimeWindow::new(Time::minutes(15), Time::minutes(30)), tw);

Trait Implementations§

Source§

impl Clone for TimeWindow

Source§

fn clone(&self) -> TimeWindow

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 Debug for TimeWindow

Source§

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

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

impl Default for TimeWindow

Source§

fn default() -> TimeWindow

Returns the “default value” for a type. Read more
Source§

impl From<(Time, Time)> for TimeWindow

Source§

fn from((start, end): (Time, Time)) -> Self

Converts to this type from the input type.
Source§

impl From<TimeWindow> for (Time, Time)

Source§

fn from(tw: TimeWindow) -> Self

Converts to this type from the input type.
Source§

impl Hash for TimeWindow

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for TimeWindow

Source§

fn eq(&self, other: &TimeWindow) -> 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 Copy for TimeWindow

Source§

impl Eq for TimeWindow

Source§

impl StructuralPartialEq for TimeWindow

Auto Trait Implementations§

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