Interval

Enum Interval 

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

https://en.wikipedia.org/wiki/Interval_(mathematics)

Variants§

§

Empty

The empty set of values.

https://en.wikipedia.org/wiki/Empty_set

§

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

Implementations§

Source§

impl<T> Interval<T>

Source

pub fn as_ref_bounds( &self, ) -> Result<(Endpoint<LEFT, &T>, Endpoint<RIGHT, &T>), <Interval<&T> as IntoBounds<&T>>::Error>
where for<'a> Interval<&'a T>: IntoBounds<&'a T>,

Get referenced interval’s bounds.

§Errors

EmptyIntervalError wrapping a reference to itself when the interval is empty.

Source§

impl<T> Interval<T>

Source

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.

Source

pub const fn as_ref(&self) -> Interval<&T>

Get an Interval with referenced bound values.

Source

pub fn reverse(self) -> Self

Swap the Interval’s bounds.

Source

pub fn into_closure(self) -> Self

Transform the Interval into a closed one by augmenting it with its endpoints.

Source

pub fn into_interior(self) -> Self
where Self: Bounded<T> + SingletonBounds<T>,

Transform the Interval into an open one by excluding its endpoint values.

Source

pub fn reduce(self) -> Self
where 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);
Source

pub fn is_empty(&self) -> bool
where 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());
Source

pub const fn is_full(&self) -> bool

Whether the Interval contains every possible value.

Source

pub fn contains<U>(&self, point: &U) -> bool
where T: PartialOrd + PartialOrd<U>, U: ?Sized + PartialOrd<T>,

Whether the Interval contains a given point.

Source

pub fn len(&self) -> Size<<T as Sub>::Output>
where T: Clone + PartialOrd + Sub,

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

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 the upper;
  • Ok((Equal, x)) if the resulted value is exactly x;
  • Ok((Greater, lower)) if the resulted value should be greater than the lower.
§Errors

Returns wrapped original value if the interval is empty;

Source§

impl<T> Interval<T>

Source

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.

Source§

impl<T> Interval<T>
where Self: Bounded<T>,

Source

pub fn complement(self) -> OneOrPair<Self>

Compute the complement of this interval.

If the Interval has two endpoints, the complement will be represented as a pair of intervals (one to the left of the start, one to the right of the end).

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

Source§

type Output = Interval<Z>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Interval<U>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Add<Output = T> + Clone> Add<T> for Interval<T>

Source§

type Output = Interval<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> Self::Output

Performs the + operation. Read more
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>,

Source§

type Output = Interval<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: U) -> Self::Output

Performs the & operation. Read more
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>,

Source§

type Output = OneOrPair<Interval<T>>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: U) -> Self::Output

Performs the | operation. Read more
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>,

Source§

type Output = OneOrPair<Interval<T>>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: U) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T> Bounded<T> for Interval<T>
where T: PartialOrd, Self: SingletonBounds<T>,

Source§

fn from_bounds(bounds: (Endpoint<LEFT, T>, Endpoint<RIGHT, T>)) -> Self

Create from the given pair of Endpoint-s.
Source§

impl<T: Clone> Clone for Interval<T>

Source§

fn clone(&self) -> Interval<T>

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> Debug for Interval<T>

Source§

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

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

impl<T: Display> Display for Interval<T>

Source§

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

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

impl<T, N, Z> Div<N> for Interval<T>
where N: Clone + Zero, T: PartialOrd + Div<N, Output = Z>, Z: PartialOrd + Zero,

Source§

type Output = Interval<Z>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: N) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> From<(Endpoint<LEFT, T>, Endpoint<RIGHT, T>)> for Interval<T>
where Self: Bounded<T>,

Source§

fn from(bounds: (Endpoint<LEFT, T>, Endpoint<RIGHT, T>)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<EmptyIntervalError<T>> for Interval<T>

Source§

fn from(err: EmptyIntervalError<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Range<T>> for Interval<T>

Source§

fn from(_: Range<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<RangeFrom<T>> for Interval<T>

Source§

fn from(_: RangeFrom<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<RangeFull> for Interval<T>

Source§

fn from(_: RangeFull) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(value: RangeInclusive<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<RangeTo<T>> for Interval<T>

Source§

fn from(_: RangeTo<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<RangeToInclusive<T>> for Interval<T>

Source§

fn from(_: RangeToInclusive<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialOrd + Hash> Hash for Interval<T>

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<'a, T> IntoBounds<&'a T> for &'a Interval<T>
where T: PartialOrd,

Source§

type Error = EmptyIntervalError<&'a T>

The error signalling conversion to Endpoint-s fails.
Source§

fn into_bounds( self, ) -> Result<(Endpoint<LEFT, &'a T>, Endpoint<RIGHT, &'a T>), Self::Error>

Convert this value into the left and right bounds, consuming the value. Read more
Source§

impl<T> IntoBounds<T> for Interval<T>
where T: PartialOrd, Self: SingletonBounds<T>,

Source§

type Error = EmptyIntervalError<T>

The error signalling conversion to Endpoint-s fails.
Source§

fn into_bounds( self, ) -> Result<(Endpoint<LEFT, T>, Endpoint<RIGHT, T>), Self::Error>

Convert this value into the left and right bounds, consuming the value. Read more
Source§

impl<T, N, Z> Mul<Interval<N>> for Interval<T>
where N: Clone + PartialOrd + Zero, T: Clone + PartialOrd + Mul<N, Output = Z> + Zero, Z: Ord + Zero, Interval<Z>: Bounded<Z>,

Source§

type Output = Interval<Z>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Interval<N>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T, N, Z> Mul<N> for Interval<T>
where N: Clone + Zero, T: PartialOrd + Mul<N, Output = Z>, Z: PartialOrd + Zero,

Source§

type Output = Interval<Z>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: N) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Neg<Output = T>> Neg for Interval<T>

Source§

type Output = Interval<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T> Not for Interval<T>
where Self: Bounded<T>,

Source§

type Output = OneOrPair<Interval<T>>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<T: PartialOrd> PartialEq for Interval<T>

Source§

fn eq(&self, other: &Self) -> 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> Singleton<T> for Interval<T>
where T: Clone,

Available on non-crate feature singleton only.
Source§

fn singleton(x: T) -> Self

Create a singleton set containing only the given value.
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>,

Source§

type Output = Interval<Z>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Interval<U>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Sub<Output = T> + Clone> Sub<T> for Interval<T>

Source§

type Output = Interval<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: T) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy> Copy for Interval<T>

Source§

impl<T: PartialOrd> Eq for Interval<T>

Source§

impl<T> SingletonBounds<T> for Interval<T>

Available on non-crate feature singleton only.

Auto Trait Implementations§

§

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

§

impl<T> RefUnwindSafe for Interval<T>
where T: RefUnwindSafe,

§

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

§

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

§

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

§

impl<T> UnwindSafe for Interval<T>
where 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, 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.