Skip to content

Commit

Permalink
fmt fix
Browse files Browse the repository at this point in the history
  • Loading branch information
urmaul committed Oct 9, 2023
1 parent 35e4f70 commit d243408
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
//! ```
use core::fmt;
use std::cmp::{max, min};
use std::cmp::max;
use std::cmp::min;
use std::cmp::Ordering;
use std::error::Error;
use std::fmt::Debug;
Expand Down Expand Up @@ -567,22 +568,56 @@ impl TimeWindow {
self.start < that.end && that.start < self.end
}

/// Returns time window that is an intersection between this time window and another one.
/// Returns None if time windows don't overlap.
/// Returns time window that is an intersection between this time window and
/// another one. Returns None if time windows don't overlap.
/// # Examples
///
/// ```
/// # use tinytime::TimeWindow;
/// 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");
/// 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"
/// );
/// ```
pub fn intersect(&self, that: &TimeWindow) -> Option<TimeWindow> {
let start = max(self.start, that.start);
Expand Down

0 comments on commit d243408

Please sign in to comment.