pub enum Interval<T> {
Empty,
LessThan(T),
LessThanOrEqual(T),
GreaterThanOrEqual(T),
GreaterThan(T),
Open((T, T)),
LeftOpen((T, T)),
RightOpen((T, T)),
Closed((T, T)),
Full,
}Expand description
Represent the subset of domain values for some T: PartialOrd.
Variants§
Empty
The empty set of values.
LessThan(T)
The set of values strictly less than the boundary.
LessThanOrEqual(T)
The set of values less or equal than the boundary.
GreaterThanOrEqual(T)
The set of values greater or equal than the boundary.
GreaterThan(T)
The set of values strictly greater than the boundary.
Open((T, T))
The set of values in open range (a, b) (both boundaries are excluded).
LeftOpen((T, T))
The set of values in half-open (a, b] range bounded exclusively below and inclusively above.
RightOpen((T, T))
The set of values in half-open [a, b) range bounded inclusively below and exclusively above.
Closed((T, T))
The set of values in closed range [a, b] (both boundaries are included).
Full
Interval containing any value.
Implementations§
Source§impl<T> Interval<T>
impl<T> Interval<T>
Sourcepub fn as_ref_bounds(
&self,
) -> Result<(Endpoint<LEFT, &T>, Endpoint<RIGHT, &T>), <Interval<&T> as IntoBounds<&T>>::Error>
pub fn as_ref_bounds( &self, ) -> Result<(Endpoint<LEFT, &T>, Endpoint<RIGHT, &T>), <Interval<&T> as IntoBounds<&T>>::Error>
Get referenced interval’s bounds.
§Errors
EmptyIntervalError wrapping a reference to itself when the interval is empty.
Source§impl<T> Interval<T>
impl<T> Interval<T>
Sourcepub fn map<U, F>(self, f: F) -> Interval<U>where
F: FnMut(T) -> U,
pub fn map<U, F>(self, f: F) -> Interval<U>where
F: FnMut(T) -> U,
Apply a transformation to the borders of an Interval,
preserving its structure.
Sourcepub fn into_closure(self) -> Self
pub fn into_closure(self) -> Self
Transform the Interval into a closed one
by augmenting it with its endpoints.
Sourcepub fn into_interior(self) -> Selfwhere
Self: Bounded<T> + SingletonBounds<T>,
pub fn into_interior(self) -> Selfwhere
Self: Bounded<T> + SingletonBounds<T>,
Transform the Interval into an open one
by excluding its endpoint values.
Sourcepub fn reduce(self) -> Selfwhere
T: PartialOrd,
pub fn reduce(self) -> Selfwhere
T: PartialOrd,
Reduce the Interval to its minimal form.
assert_eq!(Interval::Open((5, 5)).reduce(), Interval::Empty);
assert_eq!(Interval::Closed((5, 5)).reduce(), Interval::singleton(5));
assert_eq!(Interval::Closed((7, 5)).reduce(), Interval::Empty);Sourcepub fn is_empty(&self) -> boolwhere
T: PartialOrd,
pub fn is_empty(&self) -> boolwhere
T: PartialOrd,
Whether the Interval contains no valid values,
i.e. is Self::Empty or have invalid bounds (a > b).
assert!(Interval::<u32>::Empty.is_empty());
assert!(interval!((5, 5)).is_empty());
assert!(!interval!([5, 5]).is_empty());
assert!(interval!([7, 5]).is_empty());Sourcepub fn len(&self) -> Size<<T as Sub>::Output>
pub fn len(&self) -> Size<<T as Sub>::Output>
Returns the length of the Interval if both bounds are finite.
assert_eq!(interval!(0: u8).len().into_diff().unwrap(), 0);
assert!(interval!(< 5).len().into_diff().is_none());
assert!(interval!(<= 3.2).len().into_diff().is_none());
assert!(interval!(> -10).len().into_diff().is_none());
assert!(interval!(>= 0u32).len().into_diff().is_none());
assert_eq!(interval!(= 42).len().into_diff().unwrap(), 0);
assert_eq!(interval!(== 42).len().into_diff().unwrap(), 0);
assert_eq!(interval!((1, 10)).len().into_diff().unwrap(), 9);
assert_eq!(interval!((0, =100)).len().into_diff().unwrap(), 100);
assert_eq!(interval!((=0, 100)).len().into_diff().unwrap(), 100);
assert_eq!(interval!((=6, 5)).len().into_diff().unwrap(), 0);
assert_eq!(interval!([0.0, 1.0]).len().into_diff().unwrap(), 1.0);
assert_eq!(interval!([5.0, 1.0]).len().into_diff().unwrap(), 0.0);
assert_eq!(interval!((=0.0, =1.0)).len().into_diff().unwrap(), 1.0);
assert!(interval!(U: i32).len().into_diff().is_none());Sourcepub fn clamp(&self, x: T) -> Result<(Ordering, T), T>where
T: Clone + PartialOrd,
pub fn clamp(&self, x: T) -> Result<(Ordering, T), T>where
T: Clone + PartialOrd,
Restrict a given value to the Interval.
§Returns
Ok((Less, upper))if the resulted value should be less than theupper;Ok((Equal, x))if the resulted value is exactlyx;Ok((Greater, lower))if the resulted value should be greater than thelower.
§Errors
Returns wrapped original value if the interval is empty;
Source§impl<T> Interval<T>
impl<T> Interval<T>
Sourcepub fn point_cmp(&self, point: &T) -> Option<Ordering>where
T: PartialOrd,
pub fn point_cmp(&self, point: &T) -> Option<Ordering>where
T: PartialOrd,
Whether the given point lies completely to the left/right of the interval, or maybe completely matches it (in a singleton case).
This should be an impl of PartialOrd<T> for Interval<T>,
but this would require an impl of PartialEq<T> for Interval<T>,
which does not make much sense.
Even worse, multiple existing implementations of
PartialEq for Interval<T> breaks
the type inference system in expressions like
interval < interval_like.into().
This lowers much of ergonomics of using compare operators.
Trait Implementations§
Source§impl<T, U, Z> Add<Interval<U>> for Interval<T>where
T: Zero + Add<U, Output = Z>,
U: Zero,
Self: IntoBounds<T>,
Interval<U>: IntoBounds<U>,
Interval<Z>: Bounded<Z>,
impl<T, U, Z> Add<Interval<U>> for Interval<T>where
T: Zero + Add<U, Output = Z>,
U: Zero,
Self: IntoBounds<T>,
Interval<U>: IntoBounds<U>,
Interval<Z>: Bounded<Z>,
Source§impl<T, U> BitAnd<U> for Interval<T>where
Self: SetOps<T> + From<<Self as IntoBounds<T>>::Error>,
T: Ord,
U: IntoBounds<T> + From<U::Error>,
impl<T, U> BitAnd<U> for Interval<T>where
Self: SetOps<T> + From<<Self as IntoBounds<T>>::Error>,
T: Ord,
U: IntoBounds<T> + From<U::Error>,
Source§impl<T, U> BitOr<U> for Interval<T>where
Self: SetOps<T> + From<<Self as IntoBounds<T>>::Error>,
T: Ord,
U: IntoBounds<T> + From<U::Error>,
impl<T, U> BitOr<U> for Interval<T>where
Self: SetOps<T> + From<<Self as IntoBounds<T>>::Error>,
T: Ord,
U: IntoBounds<T> + From<U::Error>,
Source§impl<T, U> BitXor<U> for Interval<T>where
Self: SetOps<T> + From<<Self as IntoBounds<T>>::Error>,
T: Ord,
U: IntoBounds<T> + From<U::Error>,
for<'a> &'a U: IntoBounds<&'a T>,
impl<T, U> BitXor<U> for Interval<T>where
Self: SetOps<T> + From<<Self as IntoBounds<T>>::Error>,
T: Ord,
U: IntoBounds<T> + From<U::Error>,
for<'a> &'a U: IntoBounds<&'a T>,
Source§impl<T> Bounded<T> for Interval<T>where
T: PartialOrd,
Self: SingletonBounds<T>,
impl<T> Bounded<T> for Interval<T>where
T: PartialOrd,
Self: SingletonBounds<T>,
Source§impl<T> From<EmptyIntervalError<T>> for Interval<T>
impl<T> From<EmptyIntervalError<T>> for Interval<T>
Source§fn from(err: EmptyIntervalError<T>) -> Self
fn from(err: EmptyIntervalError<T>) -> Self
Source§impl<T> From<RangeInclusive<T>> for Interval<T>
impl<T> From<RangeInclusive<T>> for Interval<T>
Source§fn from(value: RangeInclusive<T>) -> Self
fn from(value: RangeInclusive<T>) -> Self
Source§impl<T> From<RangeToInclusive<T>> for Interval<T>
impl<T> From<RangeToInclusive<T>> for Interval<T>
Source§fn from(_: RangeToInclusive<T>) -> Self
fn from(_: RangeToInclusive<T>) -> Self
Source§impl<'a, T> IntoBounds<&'a T> for &'a Interval<T>where
T: PartialOrd,
impl<'a, T> IntoBounds<&'a T> for &'a Interval<T>where
T: PartialOrd,
Source§impl<T> IntoBounds<T> for Interval<T>where
T: PartialOrd,
Self: SingletonBounds<T>,
impl<T> IntoBounds<T> for Interval<T>where
T: PartialOrd,
Self: SingletonBounds<T>,
Source§impl<T: PartialOrd> PartialEq for Interval<T>
impl<T: PartialOrd> PartialEq for Interval<T>
Source§impl<T> Singleton<T> for Interval<T>where
T: Clone,
Available on non-crate feature singleton only.
impl<T> Singleton<T> for Interval<T>where
T: Clone,
singleton only.Source§impl<T, U, Z> Sub<Interval<U>> for Interval<T>where
T: Zero + Sub<U, Output = Z>,
U: Zero,
Self: IntoBounds<T>,
Interval<U>: IntoBounds<U>,
Interval<Z>: Bounded<Z>,
impl<T, U, Z> Sub<Interval<U>> for Interval<T>where
T: Zero + Sub<U, Output = Z>,
U: Zero,
Self: IntoBounds<T>,
Interval<U>: IntoBounds<U>,
Interval<Z>: Bounded<Z>,
impl<T: Copy> Copy for Interval<T>
impl<T: PartialOrd> Eq for Interval<T>
impl<T> SingletonBounds<T> for Interval<T>
singleton only.