pub enum WillowRange<T> {
Closed(Range<T>),
Open(RangeFrom<T>),
}Expand description
A continuous subset of a type implementing Ord.
It is either a closed range consisting of an inclusive start value and an exclusive end value, or an open range consisting only of an inclusive start value.
use std::ops::RangeBounds;
use willow_data_model::prelude::*;
assert!(WillowRange::<u8>::from(4..9).contains(&6));
assert!(!WillowRange::<u8>::from(4..9).contains(&12));
// You can create `WillowRanges` from arbitrary Rust ranges.
assert!(WillowRange::<u8>::from(4..=8) == WillowRange::<u8>::from(4..9));Variants§
Closed(Range<T>)
A closed range with an inclusive start value and an exclusive end value.
Open(RangeFrom<T>)
An open range with an inclusive start value.
Implementations§
Source§impl<T> WillowRange<T>
impl<T> WillowRange<T>
Sourcepub fn includes_range<R: RangeBounds<T>>(&self, other: &R) -> bool
pub fn includes_range<R: RangeBounds<T>>(&self, other: &R) -> bool
Returns whether every possible item that is included in other is also included in self.
If other is a WillowRange, you can equivalently check self >= other.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from(4..9).includes_range(&(5..7)), true);
assert_eq!(WillowRange::<u8>::from(4..9).includes_range(&(5..11)), false);
assert_eq!(WillowRange::<u8>::from(4..9).includes_range(&(2..7)), false);
assert_eq!(WillowRange::<u8>::from(4..9).includes_range(&(4..9)), true);Sourcepub fn strictly_includes_range<R: RangeBounds<T>>(&self, other: &R) -> bool
pub fn strictly_includes_range<R: RangeBounds<T>>(&self, other: &R) -> bool
Returns whether every possible item that is included in other is also included in self and there exists at least one item included in self but not included in other.
If other is a WillowRange, you can equivalently check self > other.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from(4..9).strictly_includes_range(&(5..7)), true);
assert_eq!(WillowRange::<u8>::from(4..9).strictly_includes_range(&(5..11)), false);
assert_eq!(WillowRange::<u8>::from(4..9).strictly_includes_range(&(2..7)), false);
assert_eq!(WillowRange::<u8>::from(4..9).strictly_includes_range(&(4..9)), false);
assert_eq!(WillowRange::<u8>::from(4..9).strictly_includes_range(&(5..9)), true);
assert_eq!(WillowRange::<u8>::from(4..9).strictly_includes_range(&(4..8)), true);Sourcepub fn does_intersection_include_value<R: RangeBounds<T>>(
&self,
other: &R,
value: &T,
) -> bool
pub fn does_intersection_include_value<R: RangeBounds<T>>( &self, other: &R, value: &T, ) -> bool
Returns whether the intersection of self and other includes the given value.
More efficient than actually creating the intersection and then calling includes.
use willow_data_model::prelude::*;
assert!(WillowRange::<u8>::from(4..9).does_intersection_include_value(&(5..7), &6));
assert!(!WillowRange::<u8>::from(4..9).does_intersection_include_value(&(5..7), &4));
assert!(!WillowRange::<u8>::from(4..9).does_intersection_include_value(&(5..7), &8));
assert!(!WillowRange::<u8>::from(4..9).does_intersection_include_value(&(5..7), &99));Source§impl<T> WillowRange<T>
impl<T> WillowRange<T>
Sourcepub fn from_bounds<R>(bounds: &R) -> Selfwhere
R: RangeBounds<T>,
pub fn from_bounds<R>(bounds: &R) -> Selfwhere
R: RangeBounds<T>,
Converts any value which implements RangeBounds<T> into a WillowRange<T> containing the same values.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from_bounds(&(..)), (..).into());
assert_eq!(WillowRange::<u8>::from_bounds(&(0..)), (0..).into());
assert_eq!(WillowRange::<u8>::from_bounds(&(..254)), (..254).into());
assert_eq!(WillowRange::<u8>::from_bounds(&(..255)), (..255).into());
assert_eq!(WillowRange::<u8>::from_bounds(&(..=254)), (..=254).into());
assert_eq!(WillowRange::<u8>::from_bounds(&(..=255)), (..=255).into());
assert_eq!(WillowRange::<u8>::from_bounds(&(4..8)), (4..8).into());
assert_eq!(WillowRange::<u8>::from_bounds(&(4..=8)), (4..=8).into());Sourcepub fn intersection<R: RangeBounds<T>>(&self, other: &R) -> Self
pub fn intersection<R: RangeBounds<T>>(&self, other: &R) -> Self
Returns the intersection of self and other, i.e., the WillowRange which includes exactly those values included in both self and other.
If you want to check whether some value is included in the intersecion of two ranges, prefer WillowRange::does_intersection_include_value over explicitly constructing the intersection.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from(4..9).intersection(&(5..7)), (5..7).into());
assert_eq!(WillowRange::<u8>::from(4..9).intersection(&(5..11)), (5..9).into());
assert_eq!(WillowRange::<u8>::from(4..9).intersection(&(2..7)), (4..7).into());
assert!(WillowRange::<u8>::from(4..9).intersection(&(11..14)).is_empty());
assert!(WillowRange::<u8>::from(4..9).intersection(&(6..6)).is_empty());Source§impl<T> WillowRange<T>
impl<T> WillowRange<T>
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns whether self is a closed range.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from(3..8).is_closed(), true);
assert_eq!(WillowRange::<u8>::from(3..255).is_closed(), true);
assert_eq!(WillowRange::<u8>::from(3..=255).is_closed(), false);
assert_eq!(WillowRange::<u8>::from(3..).is_closed(), false);Sourcepub fn is_open(&self) -> bool
pub fn is_open(&self) -> bool
Returns whether self is an open range.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from(3..8).is_open(), false);
assert_eq!(WillowRange::<u8>::from(3..255).is_open(), false);
assert_eq!(WillowRange::<u8>::from(3..=255).is_open(), true);
assert_eq!(WillowRange::<u8>::from(3..).is_open(), true);Sourcepub fn start(&self) -> &T
pub fn start(&self) -> &T
Returns a reference to the start value of self.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from(3..8).start(), &3);
assert_eq!(WillowRange::<u8>::from(..8).start(), &0);Sourcepub fn start_mut(&mut self) -> &mut T
pub fn start_mut(&mut self) -> &mut T
Returns a mutable reference to the start value of self.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from(3..8).start_mut(), &mut 3);
assert_eq!(WillowRange::<u8>::from(..8).start_mut(), &mut 0);Sourcepub fn end_mut(&mut self) -> Option<&mut T>
pub fn end_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the end value of self, or None if the range is open.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from(3..8).end_mut(), Some(&mut 8));
assert_eq!(WillowRange::<u8>::from(3..=8).end_mut(), Some(&mut 9));
assert_eq!(WillowRange::<u8>::from(3..).end_mut(), None);Sourcepub fn map<U, F>(self, f: F) -> WillowRange<U>where
F: FnMut(T) -> U,
pub fn map<U, F>(self, f: F) -> WillowRange<U>where
F: FnMut(T) -> U,
Converts this range into a different range by applying a function to all endpoints.
use willow_data_model::prelude::*;
assert_eq!(
WillowRange::<u8>::from(3..8).map(|x| (x + 1) as u16),
WillowRange::<u16>::from(4..9),
);Sourcepub unsafe fn cast<U>(&self) -> &WillowRange<U>
pub unsafe fn cast<U>(&self) -> &WillowRange<U>
Unsafely reinterprets a reference to a range as a reference to a range of a different type of boundaries.
§Safety
This is safe only if &T and &U have an identical layout in memory.
Source§impl<T> WillowRange<T>where
T: Ord,
impl<T> WillowRange<T>where
T: Ord,
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether self is empty, i.e., whether it includes no values.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::from(4..5).is_empty(), false);
assert_eq!(WillowRange::<u8>::from(255..).is_empty(), false);
assert_eq!(WillowRange::<u8>::from(4..4).is_empty(), true);
assert_eq!(WillowRange::<u8>::from(4..3).is_empty(), true);Source§impl<T> WillowRange<T>where
T: SuccessorExceptForGreatest,
impl<T> WillowRange<T>where
T: SuccessorExceptForGreatest,
Sourcepub fn singleton(t: T) -> Self
pub fn singleton(t: T) -> Self
Returns the WillowRange which includes t but no other value.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::singleton(5), (5..6).into());
assert_eq!(WillowRange::<u8>::singleton(255), (255..).into());Source§impl<T> WillowRange<T>where
T: LeastElement,
impl<T> WillowRange<T>where
T: LeastElement,
Sourcepub fn full() -> Self
pub fn full() -> Self
Returns the WillowRange which includes every value of type T.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::full(), (..).into());Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns whether self is the full range, i.e., the range which includes every value of type T.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::full().is_full(), true);
assert_eq!(WillowRange::<u8>::from(1..).is_full(), false);
assert_eq!(WillowRange::<u8>::from(0..255).is_full(), false);Trait Implementations§
Source§impl<'a, T: Arbitrary<'a>> Arbitrary<'a> for WillowRange<T>
Available on crate feature dev only.
impl<'a, T: Arbitrary<'a>> Arbitrary<'a> for WillowRange<T>
dev only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§impl<T: Clone> Clone for WillowRange<T>
impl<T: Clone> Clone for WillowRange<T>
Source§fn clone(&self) -> WillowRange<T>
fn clone(&self) -> WillowRange<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for WillowRange<T>
impl<T: Debug> Debug for WillowRange<T>
Source§impl<T> From<Range<T>> for WillowRange<T>
impl<T> From<Range<T>> for WillowRange<T>
Source§impl<T> From<RangeFrom<T>> for WillowRange<T>
impl<T> From<RangeFrom<T>> for WillowRange<T>
Source§impl<T> From<RangeFull> for WillowRange<T>where
T: LeastElement,
impl<T> From<RangeFull> for WillowRange<T>where
T: LeastElement,
Source§impl<T> From<RangeInclusive<T>> for WillowRange<T>where
T: SuccessorExceptForGreatest,
impl<T> From<RangeInclusive<T>> for WillowRange<T>where
T: SuccessorExceptForGreatest,
Source§fn from(value: RangeInclusive<T>) -> Self
fn from(value: RangeInclusive<T>) -> Self
Source§impl<T> From<RangeTo<T>> for WillowRange<T>where
T: LeastElement,
impl<T> From<RangeTo<T>> for WillowRange<T>where
T: LeastElement,
Source§impl<T> From<RangeToInclusive<T>> for WillowRange<T>where
T: LeastElement + SuccessorExceptForGreatest,
impl<T> From<RangeToInclusive<T>> for WillowRange<T>where
T: LeastElement + SuccessorExceptForGreatest,
Source§fn from(value: RangeToInclusive<T>) -> Self
fn from(value: RangeToInclusive<T>) -> Self
Source§impl<T> GreatestElement for WillowRange<T>
impl<T> GreatestElement for WillowRange<T>
Source§impl<T> Hash for WillowRange<T>
impl<T> Hash for WillowRange<T>
Source§impl<T> LeastElement for WillowRange<T>
impl<T> LeastElement for WillowRange<T>
Source§impl<T> LowerSemilattice for WillowRange<T>
impl<T> LowerSemilattice for WillowRange<T>
Source§fn greatest_lower_bound(&self, other: &Self) -> Self
fn greatest_lower_bound(&self, other: &Self) -> Self
self and other, i.e., the unique greatest element in the type which is less than or equal to both self and other.Source§impl<T> PartialEq for WillowRange<T>
Two WillowRanges are equal iff they contain the same values.
impl<T> PartialEq for WillowRange<T>
Two WillowRanges are equal iff they contain the same values.
Source§fn eq(&self, other: &WillowRange<T>) -> bool
fn eq(&self, other: &WillowRange<T>) -> bool
self and other values to be equal, and is used by ==.Source§fn ne(&self, other: &WillowRange<T>) -> bool
fn ne(&self, other: &WillowRange<T>) -> bool
!=. The default implementation is almost always sufficient,
and should not be overridden without very good reason.Source§impl<T> PartialOrd for WillowRange<T>
A range is less than another iff all values contained in the first are also contained in the other.
impl<T> PartialOrd for WillowRange<T>
A range is less than another iff all values contained in the first are also contained in the other.
Source§impl<T> RangeBounds<T> for WillowRange<T>
impl<T> RangeBounds<T> for WillowRange<T>
Source§impl<T> UpperSemilattice for WillowRange<T>
impl<T> UpperSemilattice for WillowRange<T>
Source§fn least_upper_bound(&self, other: &Self) -> Self
fn least_upper_bound(&self, other: &Self) -> Self
self and other, i.e., the unique least element in the type which is greater than or equal to both self and other.