Interval

Struct Interval 

Source
pub struct Interval<T, L = Inclusive, R = L> { /* private fields */ }
Expand description

Interval like [a, b], (a, b), [a, b), and (a, b] for any PartialOrd type.

  • T: Numeric type bounding real number line. T should implements PartialOrd. NaN safety is not guaranteed when T is floating point type.
  • L: Left boundary type. Specify one of Inclusive, Exclusive, or BoundType.
  • R: Right boundary type. Specify one of Inclusive Exclusive, or BoundType.
  • Interval<T> (= Interval<T, Inclusive, Inclusive>) represents a closed interval, i.e., [a, b].
  • Interval<T, Exclusive> (= Interval<T, Exclusive, Exclusive>) represents a open interval, i.e., (a, b).
  • Interval<T, Inclusive, Exclusive> represents a right half-open interval, i.e., [a, b).
  • Interval<T, Exclusive, Inclusive> represents a left half-open interval, i.e., (a, b].
  • Interval<T, BoundType> represents any of the above.

This type is considered as an interval on ℝ (real number line), even if an integer type is specified for T.

§Memory cost

use inter_val::{Interval, Exclusive, Inclusive, BoundType};
use std::mem::size_of;

// When bound type is statically determined, the size of the interval is just the size of two `T`.
assert_eq!(size_of::<Interval<i32, Inclusive>>(), size_of::<i32>() * 2);
assert_eq!(size_of::<Interval<f64, Exclusive>>(), size_of::<f64>() * 2);

// Size is larger when bound type is not statically determined.
assert!(size_of::<Interval<i32, BoundType>>() >= (size_of::<i32>() + size_of::<BoundType>()) * 2);

§Properties

lower_bound     left              . center          right    upper_bound
...------------>|<------- self -------------------->|<------------ ...
                inf                                 sup
                [<------------ closure ------------>]
                 (<----------- interior ---------->)

§Set operations

|<------------- a ----------------->|   . p           |<-------- c -------->|
       |<--------------- b ------------------->|
       |<--- a.intersection(&b) --->|
                                    |<-- a.gap(&c) -->|
|<------------- a.hull(p) ------------->|
|<---------------------------------- a.span(&c) --------------------------->|
|<--------------------------------->|        +        |<------------------->| a.union(&c)
|<---->| a.difference(&b)
                                               |<- δ -+---- c.dilate(δ) ----+- δ ->|

Implementations§

Source§

impl<T, L, R> Interval<T, L, R>

Source

pub fn left(&self) -> &LeftBounded<T, L>

Source

pub fn right(&self) -> &RightBounded<T, R>

Source§

impl<T: PartialOrd, L: BoundaryOf<Left>, R: BoundaryOf<Right>> Interval<T, L, R>

Source

pub fn try_new(left: Bound<T, L>, right: Bound<T, R>) -> Option<Self>

Try to create a new interval. Return None if the interval is empty.

use std::any::{Any, TypeId};
use inter_val::{Interval, BoundType, Exclusive, Inclusive};

let a: Interval<i32, Inclusive, Exclusive> = Interval::try_new(0.into(), 3.into()).unwrap();
assert!(a.contains(&0));
assert!(a.contains(&2));
assert!(!a.contains(&3));

let a = Interval::try_new(Exclusive.at(0), Inclusive.at(3)).unwrap();
assert_eq!(a.type_id(), TypeId::of::<Interval<i32, Exclusive, Inclusive>>());

let a = Interval::try_new(BoundType::Exclusive.at(0), BoundType::Exclusive.at(3)).unwrap();
assert_eq!(a.type_id(), TypeId::of::<Interval<i32, BoundType, BoundType>>());

assert!(Interval::try_new(Inclusive.at(3), Exclusive.at(0)).is_none()); // [3, 0) is empty.
assert!(Interval::try_new(Inclusive.at(3), Exclusive.at(3)).is_none()); // [3, 3) is empty.
assert!(Interval::try_new(Inclusive.at(3), Inclusive.at(3)).is_some()); // [3, 3] is not empty.
assert!(Interval::try_new(Exclusive.at(0), Exclusive.at(1)).is_some()); // (0, 1) is not empty.
Source

pub fn new(left: Bound<T, L>, right: Bound<T, R>) -> Self

Create a new interval. Panics if the interval is empty.

use std::any::{Any, TypeId};
use inter_val::{Interval, BoundType, Exclusive, Inclusive};

let a: Interval<i32, Inclusive, Exclusive> = Interval::new(0.into(), 3.into());
assert!(a.contains(&0));
assert!(a.contains(&2));
assert!(!a.contains(&3));

let a = Interval::new(Exclusive.at(0), Inclusive.at(3));
assert_eq!(a.type_id(), TypeId::of::<Interval<i32, Exclusive, Inclusive>>());

let a = Interval::new(BoundType::Exclusive.at(0), BoundType::Exclusive.at(3));
assert_eq!(a.type_id(), TypeId::of::<Interval<i32, BoundType, BoundType>>());
§Panics
Interval::new(Inclusive.at(3), Exclusive.at(0)); // [3, 0) is empty.
Interval::new(Inclusive.at(3), Exclusive.at(3)); // [3, 3) is empty.
Source

pub fn try_between(a: T, b: T) -> Option<Self>
where T: Into<Bound<T, L>> + Into<Bound<T, R>>,

use inter_val::{Interval, Exclusive, Inclusive};
let a: Interval<i32, Inclusive, Exclusive> = Interval::try_between(-2, 5).unwrap();
assert_eq!(a, Inclusive.at(-2).to(Exclusive.at(5)));

let a: Interval<i32, Inclusive, Exclusive> = Interval::try_between(3, -1).unwrap();
assert_eq!(a, Inclusive.at(-1).to(Exclusive.at(3))); // Swaps left and right.

assert!(Interval::<i32, Inclusive, Exclusive>::try_between(1, 1).is_none()); // [1, 1) is empty.
assert!(Interval::<i32, Inclusive, Inclusive>::try_between(1, 1).is_some()); // [1, 1] is not empty.
Source

pub fn between(a: T, b: T) -> Self
where T: Into<Bound<T, L>> + Into<Bound<T, R>>,

use inter_val::{Interval, Exclusive, Inclusive};
let a: Interval<i32, Inclusive, Exclusive> = Interval::between(-2, 5);
assert_eq!(a, Inclusive.at(-2).to(Exclusive.at(5)));

let a: Interval<i32, Inclusive, Exclusive> = Interval::between(3, -1);
assert_eq!(a, Inclusive.at(-1).to(Exclusive.at(3))); // Swaps left and right.

// Closed interval (bounded by `Inclusive`) never panics.
Interval::<i32, Inclusive, Inclusive>::between(1, 1); // Doesn't panic since [1, 1] is not empty.
Interval::<i32, Inclusive, Exclusive>::between(1, 1); // Panics since [1, 1) is empty.
Source

pub fn inf(&self) -> &T

Shorthand of .left().limit

use inter_val::{Interval, Exclusive, Inclusive};
let a = Interval::new(Exclusive.at(-1.0), Inclusive.at(1.0));
assert_eq!(a.inf(), &-1.0);
assert_eq!(a.inf(), &a.left().limit);
assert!(!a.contains(&-1.0));
Source

pub fn sup(&self) -> &T

Shorthand of .right().limit

use inter_val::{Interval, Exclusive, Inclusive};
let a = Interval::new(Inclusive.at(-1.0), Exclusive.at(1.0));
assert_eq!(a.sup(), &1.0);
assert_eq!(a.sup(), &a.right().limit);
assert!(!a.contains(&1.0));
Source

pub fn closure(self) -> Interval<T, Inclusive>

Source

pub fn interior(self) -> Option<Interval<T, Exclusive>>

Source

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

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(4).to(Exclusive.at(7));
let b = Exclusive.at(1.23).to(Inclusive.at(4.56));
assert!(a.contains(&4));
assert!(!a.contains(&7));
assert!(!b.contains(&1.23));
assert!(b.contains(&1.230000000001));
assert!(b.contains(&4.56));
Source

pub fn dilate(self, delta: T) -> Self
where T: Clone + Add<Output = T> + Sub<Output = T>,

use inter_val::{Inclusive, Exclusive};
let a = Inclusive.at(4).to(Exclusive.at(7));    // [4, 7)
assert_eq!(a.dilate(2), Inclusive.at(2).to(Exclusive.at(9)));   // [4-2, 7+2) = [2, 9)
assert_eq!(a.dilate(-1), Inclusive.at(5).to(Exclusive.at(6)));  // [4+1, 7-1) = [5, 6)
use inter_val::{Inclusive, Exclusive};
let a = Inclusive.at(4).to(Exclusive.at(7));    // [4, 7)
a.dilate(-2);   // panic! [4+2, 7-2) = [6, 5) is empty.
Source

pub fn includes(&self, other: &Self) -> bool

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));
let b = Inclusive.at(0).to(Exclusive.at(4));
let c = Inclusive.at(1).to(Exclusive.at(4));
assert!(a.includes(&a));
assert!(!a.includes(&b) && b.includes(&a));
assert!(!a.includes(&c) && !c.includes(&a));
Source

pub fn overlaps(&self, other: &Self) -> bool

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));
let b = Inclusive.at(1).to(Exclusive.at(4));
let c = Inclusive.at(3).to(Exclusive.at(5));
assert!(a.overlaps(&a));
assert!(a.overlaps(&b) && b.overlaps(&a));
assert!(!a.overlaps(&c) && !c.overlaps(&a));
Source

pub fn intersection(&self, other: &Self) -> Option<Self>
where T: Clone,

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));
let b = Inclusive.at(1).to(Exclusive.at(4));
let c = Inclusive.at(3).to(Exclusive.at(5));
assert_eq!(a.intersection(&a), Some(a));
assert_eq!(a.intersection(&b), Some(Inclusive.at(1).to(Exclusive.at(3))));
assert_eq!(a.intersection(&c), None);
Source

pub fn span(&self, other: &Self) -> Self
where T: Clone,

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));
let b = Inclusive.at(5).to(Exclusive.at(8));
assert_eq!(a.span(&b), Inclusive.at(0).to(Exclusive.at(8)));
Source

pub fn hull(self, t: T) -> Self
where T: Clone,

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));
assert_eq!(a.hull(-2), Inclusive.at(-2).to(Exclusive.at(3)));
assert_eq!(a.hull(5), Inclusive.at(0).to(Exclusive.at(5)));
Source

pub fn gap(&self, other: &Self) -> Option<Interval<T, R::Flip, L::Flip>>
where T: Clone, L::Flip: BoundaryOf<Right>, R::Flip: BoundaryOf<Left>,

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));
let b = Inclusive.at(5).to(Exclusive.at(8));
assert_eq!(a.gap(&b).unwrap(), Inclusive.at(3).to(Exclusive.at(5)));
Source

pub fn union(&self, other: &Self) -> IntervalUnion<T, L, R>
where T: Clone, L::Flip: BoundaryOf<Right>, R::Flip: BoundaryOf<Left>,

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));
let b = Inclusive.at(5).to(Exclusive.at(8));
let union = a.union(&b);
assert_eq!(union.span, a.span(&b));
assert_eq!(union.gap, a.gap(&b));
let union_ints: Vec<Interval<_, _, _>> = union.into_iter().collect();
assert_eq!(union_ints.len(), 2);
assert_eq!(union_ints[0], a);
assert_eq!(union_ints[1], b);
Source

pub fn lower_bound(&self) -> RightBounded<T, L::Flip>
where T: Clone,

Source

pub fn upper_bound(&self) -> LeftBounded<T, R::Flip>
where T: Clone,

Source

pub fn measure(&self) -> T
where T: Clone + Sub<Output = T>,

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(2.1).to(Inclusive.at(5.3));
assert_eq!(a.measure(), 5.3 - 2.1);

let a = Exclusive.at(0).to(Exclusive.at(1));    // (0, 1)
assert_eq!(a.measure(), 1);
Source

pub fn step_by(&self, step: T) -> impl Iterator<Item = T> + '_
where for<'a> T: Clone + AddAssign<&'a T>,

use inter_val::{Inclusive, Exclusive};
let a = Exclusive.at(10).to(Inclusive.at(20)); // (10, 20]
assert!(a.step_by(2).eq(vec![12, 14, 16, 18, 20]));
Source

pub fn step_rev_by(&self, step: T) -> impl Iterator<Item = T> + '_
where for<'a> T: Clone + SubAssign<&'a T>,

use inter_val::{Inclusive, Exclusive};
let a = Exclusive.at(10).to(Inclusive.at(20)); // (10, 20]
assert!(a.step_rev_by(2).eq(vec![20, 18, 16, 14, 12]));
Source

pub fn span_many<A: Borrow<Self>>( items: impl IntoIterator<Item = A>, ) -> Option<Self>
where T: Clone,

use inter_val::{Interval, Inclusive, Exclusive, Nullable};
let a = Inclusive.at(0).to(Exclusive.at(3));  // [0, 3)
let b = Inclusive.at(1).to(Exclusive.at(5));  // [1, 5)
let c = Inclusive.at(8).to(Exclusive.at(10)); // [8, 10)
let span = Interval::span_many(vec![a, b, c]).unwrap(); // [0, 10)
assert_eq!(span.left().limit, 0);
assert_eq!(span.right().limit, 10);

// Sum for Nullable<Interval> can be used as well.
let sum: Nullable<Interval<_, _, _>> = vec![a, b, c].into_iter().sum();
assert_eq!(sum.unwrap(), span);
Source

pub fn hull_many(items: impl IntoIterator<Item = T>) -> Option<Self>
where T: Clone + Into<Bound<T, L>> + Into<Bound<T, R>>,

use inter_val::{Interval, Nullable};
let hull = Interval::<_>::hull_many(vec![3, 9, 2, 5]).unwrap(); // [2, 9]
assert_eq!(hull.inf(), &2);
assert_eq!(hull.sup(), &9);

let hull = Interval::<_>::hull_many(vec![3.1, 9.2, 2.3, 5.4]).unwrap(); // [2.3, 9.2]
assert_eq!(hull.inf(), &2.3);
assert_eq!(hull.sup(), &9.2);

// Sum for Nullable<Interval> can be used as well.
let a: Nullable<Interval<i32>> = vec![1, 6, 2, 8, 3].into_iter().sum();
assert_eq!(a.unwrap(), Interval::between(1, 8));
Source§

impl<T: PartialOrd, L: BoundaryOf<Left, Flip = R>, R: BoundaryOf<Right, Flip = L>> Interval<T, L, R>

Source

pub fn difference(&self, other: &Self) -> IntervalDifference<T, L, R>
where T: Clone,

Difference is defined only for Interval<T, Inclusive, Exclusive>, Interval<T, Exclusive, Inclusive>, and Interval<T, BoundType>.

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));
let b = Inclusive.at(1).to(Exclusive.at(4));
let diff = a.difference(&b);
assert!(diff.lower.is_some() && diff.upper.is_none());
assert_eq!(diff.lower.unwrap(), Inclusive.at(0).to(Exclusive.at(1)));
assert_eq!(diff.into_iter().collect::<Vec<_>>().len(), 1);
Source§

impl<T: PartialOrd + Clone> Interval<T, Inclusive, Exclusive>

Source

pub fn try_split_at(&self, t: T) -> (Option<Self>, Option<Self>)

use inter_val::{Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));    // [0, 3)
let (b, c) = a.try_split_at(1); // [0, 1) and [1, 3)
assert_eq!(b, Some(Inclusive.at(0).to(Exclusive.at(1))));
assert_eq!(c, Some(Inclusive.at(1).to(Exclusive.at(3))));

let (b, c) = a.try_split_at(0);
assert_eq!(b, None);    // [0, 0) is empty.
assert_eq!(c, Some(a)); // [0, 3)
Source

pub fn split_at(&self, t: T) -> (Self, Self)

use inter_val::{Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));    // [0, 3)
let (b, c) = a.split_at(1); // [0, 1) and [1, 3)
assert_eq!(b, Inclusive.at(0).to(Exclusive.at(1)));
assert_eq!(c, Inclusive.at(1).to(Exclusive.at(3)));
use inter_val::{Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));    // [0, 3)
let (b, c) = a.split_at(0);
Source§

impl<T: Float, L: BoundaryOf<Left>, R: BoundaryOf<Right>> Interval<T, L, R>

Source

pub fn center(&self) -> T

use inter_val::{Interval, Inclusive};
let a = Inclusive.at(2.1).to(Inclusive.at(5.3));
assert_eq!(a.center(), (2.1 + 5.3) / 2.0);
Source

pub fn iou(&self, other: &Self) -> T

IoU - Intersection over Union.

use inter_val::{Interval, Inclusive};
let a = Inclusive.at(0.0).to(Inclusive.at(1.0));
let b = Inclusive.at(0.0).to(Inclusive.at(2.0));
let c = Inclusive.at(1.0).to(Inclusive.at(2.0));
assert_eq!(a.iou(&a), 1.0);
assert_eq!(a.iou(&b), 0.5);
assert_eq!(a.iou(&c), 0.0);
Source

pub fn lerp(&self, ratio: T) -> T

Linear interpolation.

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(2.0).to(Inclusive.at(4.0));    // [2, 4]
assert_eq!(a.lerp(0.0), 2.0);
assert_eq!(a.lerp(0.5), 3.0);
assert_eq!(a.lerp(1.0), 4.0);
assert_eq!(a.lerp(1.1), 4.2);
Source

pub fn step_uniform(&self, n: usize) -> impl Iterator<Item = T> + '_

use inter_val::{Interval, Inclusive, Exclusive};
let a = Inclusive.at(2.0).to(Inclusive.at(4.0));    // [2, 4]
let b = Inclusive.at(2.0).to(Exclusive.at(4.0));    // [2, 4)
let c = Exclusive.at(2.0).to(Inclusive.at(4.0));    // (2, 4]
assert!(a.step_uniform(4).eq(vec![2.0, 2.5, 3.0, 3.5, 4.0]));
assert!(b.step_uniform(4).eq(vec![2.0, 2.5, 3.0, 3.5]));
assert!(c.step_uniform(4).eq(vec![2.5, 3.0, 3.5, 4.0]));
Source§

impl<T, L, R> Interval<T, L, R>

Source

pub fn cast<U: From<T>>(self) -> Interval<U, L, R>

Cast by From<T>.

use inter_val::{Interval, Exclusive};
let src: Interval<i32, Exclusive> = Interval::between(0, 1);  // open interval (0, 1)
let dst = src.cast::<f64>();
assert!(dst.contains(&0.5));
Source§

impl<T: NumCast, L, R> Interval<T, L, R>

Source

pub fn try_cast<U: NumCast>(self) -> Option<Interval<U, L, R>>

Cast by num::NumCast.

use inter_val::{Interval, Exclusive};
let src: Interval<f64> = Interval::between(1.2, 7.8);  // closed interval [1.2, 7.8]
let dst = src.try_cast::<i32>().unwrap();
assert_eq!(dst.inf(), &1);
assert_eq!(dst.sup(), &7);

Trait Implementations§

Source§

impl<T: Clone, L: Clone, R: Clone> Clone for Interval<T, L, R>

Source§

fn clone(&self) -> Interval<T, L, R>

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, L: Debug, R: Debug> Debug for Interval<T, L, R>

Source§

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

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

impl<T> From<Interval<T>> for Interval<T, BoundType>

use inter_val::{BoundType, Inclusive, Interval};
let src: Interval<i32, Inclusive> = Inclusive.at(0).to(Inclusive.at(10));
let dst: Interval<i32, BoundType> = src.into();
assert_eq!(dst.left().bound_type, BoundType::Inclusive);
assert_eq!(dst.right().bound_type, BoundType::Inclusive);
Source§

fn from(i: Interval<T, Inclusive>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Interval<T>> for RangeInclusive<T>

use inter_val::{Interval, Inclusive};
let src = Inclusive.at(0).to(Inclusive.at(10));
let dst: std::ops::RangeInclusive<i32> = src.into();
assert_eq!(dst.start(), &0);
assert_eq!(dst.end(), &10);
Source§

fn from(i: Interval<T, Inclusive, Inclusive>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Interval<T, Exclusive>> for Interval<T, BoundType>

use inter_val::{BoundType, Exclusive, Interval};
let src: Interval<i32, Exclusive> = Exclusive.at(0).to(Exclusive.at(10));
let dst: Interval<i32, BoundType> = src.into();
assert_eq!(dst.left().bound_type, BoundType::Exclusive);
assert_eq!(dst.right().bound_type, BoundType::Exclusive);
Source§

fn from(i: Interval<T, Exclusive>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Interval<T, Exclusive, Inclusive>> for Interval<T, BoundType>

use inter_val::{BoundType, Inclusive, Exclusive, Interval};
let src: Interval<i32, Exclusive, Inclusive> = Exclusive.at(0).to(Inclusive.at(10));
let dst: Interval<i32, BoundType> = src.into();
assert_eq!(dst.left().bound_type, BoundType::Exclusive);
assert_eq!(dst.right().bound_type, BoundType::Inclusive);
Source§

fn from(i: Interval<T, Exclusive, Inclusive>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Interval<T, Inclusive, Exclusive>> for Interval<T, BoundType>

use inter_val::{BoundType, Inclusive, Exclusive, Interval};
let src: Interval<i32, Inclusive, Exclusive> = Inclusive.at(0).to(Exclusive.at(10));
let dst: Interval<i32, BoundType> = src.into();
assert_eq!(dst.left().bound_type, BoundType::Inclusive);
assert_eq!(dst.right().bound_type, BoundType::Exclusive);
Source§

fn from(i: Interval<T, Inclusive, Exclusive>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Interval<T, Inclusive, Exclusive>> for Range<T>

use inter_val::{Interval, Inclusive, Exclusive};
let src = Inclusive.at(0).to(Exclusive.at(10));
let dst: std::ops::Range<i32> = src.into();
assert_eq!(dst.start, 0);
assert_eq!(dst.end, 10);
Source§

fn from(i: Interval<T, Inclusive, Exclusive>) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialOrd + Clone> From<T> for Interval<T, Inclusive>

use std::any::{Any, TypeId};
use inter_val::{Inclusive, Interval};
let a: Interval<_, _, _> = 3.into();
assert_eq!(a.type_id(), TypeId::of::<Interval<i32, Inclusive, Inclusive>>());
assert_eq!(a.left().limit, 3);
assert_eq!(a.right().limit, 3);
Source§

fn from(t: T) -> Self

Converts to this type from the input type.
Source§

impl<T, L, R> IntoIterator for Interval<T, L, R>
where RangeInclusive<T>: Iterator<Item = T>, for<'a> T: Integer + Clone + AddAssign<&'a T> + SubAssign<&'a T>, L: BoundaryOf<Left>, R: BoundaryOf<Right>,

use inter_val::{Interval, Exclusive, Inclusive, BoundType};

// Iterate Interval<i32, Exclusive, Inclusive>
let items: Vec<_> = Exclusive.at(0).to(Inclusive.at(10)).into_iter().collect();
assert_eq!(items.len(), 10);
assert_eq!(items[0], 1);
assert_eq!(items.last().unwrap(), &10);

// Iterate Interval<i32, BoundType, BoundType>
let items: Vec<_> = (BoundType::Exclusive.at(0).to(BoundType::Inclusive.at(10)))
    .into_iter()
    .collect();
assert_eq!(items.len(), 10);
assert_eq!(items[0], 1);
assert_eq!(items.last().unwrap(), &10);
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = RangeInclusive<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq, L: PartialEq, R: PartialEq> PartialEq for Interval<T, L, R>

Source§

fn eq(&self, other: &Interval<T, L, R>) -> 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, L, R> Sum<Interval<T, L, R>> for Nullable<Interval<T, L, R>>
where T: PartialOrd + Clone, L: BoundaryOf<Left>, R: BoundaryOf<Right>,

use inter_val::{Nullable, Interval, Inclusive, Exclusive};
let a = Inclusive.at(0).to(Exclusive.at(3));  // [0, 3)
let b = Inclusive.at(1).to(Exclusive.at(5));  // [1, 5)
let c = Inclusive.at(8).to(Exclusive.at(10)); // [8, 10)
let span: Nullable<Interval<_, _, _>> = vec![a, b, c].into_iter().sum(); // [0, 10)
assert_eq!(span.as_ref().unwrap().left().limit, 0);
assert_eq!(span.as_ref().unwrap().right().limit, 10);
Source§

fn sum<I: Iterator<Item = Interval<T, L, R>>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<T: PartialOrd> TryFrom<Range<T>> for Interval<T, Inclusive, Exclusive>

use std::any::{Any, TypeId};
use inter_val::{Interval, Inclusive, Exclusive};
let a: Interval<_, _, _> = (2..4).try_into().unwrap();
assert_eq!(a.type_id(), TypeId::of::<Interval<i32, Inclusive, Exclusive>>());
assert_eq!(a.left().limit, 2);
assert_eq!(a.right().limit, 4);
Source§

type Error = IntervalIsEmpty

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

fn try_from(r: Range<T>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: PartialOrd> TryFrom<RangeInclusive<T>> for Interval<T, Inclusive>

use std::any::{Any, TypeId};
use inter_val::{Interval, Inclusive};
let a: Interval<_, _, _> = (2..=4).try_into().unwrap();
assert_eq!(a.type_id(), TypeId::of::<Interval<i32, Inclusive, Inclusive>>());
assert_eq!(a.left().limit, 2);
assert_eq!(a.right().limit, 4);
Source§

type Error = IntervalIsEmpty

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

fn try_from(r: RangeInclusive<T>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: Copy, L: Copy, R: Copy> Copy for Interval<T, L, R>

Source§

impl<T: Eq, L: Eq, R: Eq> Eq for Interval<T, L, R>

Source§

impl<T, L, R> StructuralPartialEq for Interval<T, L, R>

Auto Trait Implementations§

§

impl<T, L, R> Freeze for Interval<T, L, R>
where T: Freeze, L: Freeze, R: Freeze,

§

impl<T, L, R> RefUnwindSafe for Interval<T, L, R>

§

impl<T, L, R> Send for Interval<T, L, R>
where T: Send, L: Send, R: Send,

§

impl<T, L, R> Sync for Interval<T, L, R>
where T: Sync, L: Sync, R: Sync,

§

impl<T, L, R> Unpin for Interval<T, L, R>
where T: Unpin, L: Unpin, R: Unpin,

§

impl<T, L, R> UnwindSafe for Interval<T, L, R>
where T: UnwindSafe, L: UnwindSafe, R: 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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.