#[repr(C)]pub enum WillowRange<T> {
Closed(ClosedRange<T>),
Open(T),
}Expand description
A non-empty continuous subset of a type implementing PartialOrd.
It is either a closed range consisting of an inclusive start value and a strictly-greater 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>::new_closed(4, 9).contains(&6));
assert!(!WillowRange::<u8>::new_closed(4, 9).contains(&12));Variants§
Closed(ClosedRange<T>)
A closed range with an inclusive start value and an exclusive end value.
Open(T)
An open range with an inclusive start value.
It includes all values greater than or equal to that start value.
Implementations§
Source§impl<T> WillowRange<T>
impl<T> WillowRange<T>
Sourcepub fn new_open(start: T) -> Self
pub fn new_open(start: T) -> Self
Creates a new open range.
use willow_data_model::prelude::*;
let open = WillowRange::new_open(2);
assert!(open.is_open());
assert_eq!(open.start(), &2);Sourcepub fn start(&self) -> &T
pub fn start(&self) -> &T
Returns a reference to the start value of this range.
use willow_data_model::prelude::*;
let closed = WillowRange::new_closed(2, 7);
assert_eq!(closed.start(), &2);
let open = WillowRange::new_open(2);
assert_eq!(open.start(), &2);Sourcepub fn end(&self) -> Option<&T>
pub fn end(&self) -> Option<&T>
Returns a reference to the end value of this range, or None of it is open.
use willow_data_model::prelude::*;
let closed = WillowRange::new_closed(2, 7);
assert_eq!(closed.end(), Some(&7));
let open = WillowRange::new_open(2);
assert_eq!(open.end(), None);Sourcepub fn into_components(self) -> (T, Option<T>)
pub fn into_components(self) -> (T, Option<T>)
Takes ownership of a willow range and returns its start and end.
use willow_data_model::prelude::*;
let closed = WillowRange::new_closed(2, 7);
assert_eq!(closed.into_components(), (2, Some(7)));
let open = WillowRange::new_open(2);
assert_eq!(open.into_components(), (2, None));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>::new_closed(3, 8).is_closed(), true);
assert_eq!(WillowRange::<u8>::new_open(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>::new_closed(3, 8).is_open(), false);
assert_eq!(WillowRange::<u8>::new_open(3).is_open(), true);Sourcepub fn try_map<U, F>(self, f: F) -> Result<WillowRange<U>, EmptyGrouping>where
F: FnMut(T) -> U,
U: PartialOrd,
pub fn try_map<U, F>(self, f: F) -> Result<WillowRange<U>, EmptyGrouping>where
F: FnMut(T) -> U,
U: PartialOrd,
Converts this range into a different range by applying a function to all endpoints. Returns an error if the new range would be empty.
use willow_data_model::prelude::*;
assert_eq!(
WillowRange::<u8>::new_closed(3, 8).try_map(|x| (x + 1) as u16),
Ok(WillowRange::<u16>::new_closed(4, 9)),
);Sourcepub fn map<U, F>(self, f: F) -> WillowRange<U>where
F: FnMut(T) -> U,
U: PartialOrd,
pub fn map<U, F>(self, f: F) -> WillowRange<U>where
F: FnMut(T) -> U,
U: PartialOrd,
Converts this range into a different range by applying a function to all endpoints. Panics if the new range would be empty.
use willow_data_model::prelude::*;
assert_eq!(
WillowRange::<u8>::new_closed(3, 8).map(|x| (x + 1) as u16),
WillowRange::<u16>::new_closed(4, 9),
);Sourcepub unsafe fn map_unchecked<U, F>(self, f: F) -> WillowRange<U>where
F: FnMut(T) -> U,
U: PartialOrd,
pub unsafe fn map_unchecked<U, F>(self, f: F) -> WillowRange<U>where
F: FnMut(T) -> U,
U: PartialOrd,
Converts this range into a different range by applying a function to all endpoints, without checking if the resulting range would be empty.
§Safety
Undefined behaviour may occur if the resulting range would be empty.
use willow_data_model::prelude::*;
assert_eq!(
unsafe {
WillowRange::<u8>::new_closed(3, 8).map_unchecked(|x| (x + 1) as u16)
},
WillowRange::<u16>::new_closed(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, and if the resulting range is non-empty.
Sourcepub unsafe fn new_unchecked(start: T, end: Option<T>) -> Self
pub unsafe fn new_unchecked(start: T, end: Option<T>) -> Self
Creates a new Willow range, without enforcing non-emptyness.
§Safety
Calling this method with start >= end is undefined behaviour.
use willow_data_model::prelude::*;
let yay = unsafe { WillowRange::new_unchecked(2, Some(7)) };Sourcepub unsafe fn new_closed_unchecked(start: T, end: T) -> Self
pub unsafe fn new_closed_unchecked(start: T, end: T) -> Self
Creates a new closed Willow range, without enforcing non-emptyness.
§Safety
Calling this method with start >= end is undefined behaviour.
use willow_data_model::prelude::*;
let yay = unsafe { WillowRange::new_closed_unchecked(2, 7) };Source§impl<T> WillowRange<T>where
T: PartialOrd,
impl<T> WillowRange<T>where
T: PartialOrd,
Sourcepub fn try_new(start: T, end: Option<T>) -> Result<Self, EmptyGrouping>
pub fn try_new(start: T, end: Option<T>) -> Result<Self, EmptyGrouping>
Creates a new Willow range from a start value and an optional end value, or returns an EmptyGrouping error if the created range would be empty.
use willow_data_model::prelude::*;
assert!(WillowRange::try_new(2, Some(7)).is_ok());
assert!(WillowRange::try_new(7, Some(2)).is_err());
assert!(WillowRange::try_new(2, Some(2)).is_err());Sourcepub fn new(start: T, end: Option<T>) -> Self
pub fn new(start: T, end: Option<T>) -> Self
Creates a new Willow range from a start value and an optional end value, panicking if the created range would be empty.
use willow_data_model::prelude::*;
let yay = WillowRange::new_closed(2, 7);use willow_data_model::prelude::*;
let nope = WillowRange::new(7, Some(2));Sourcepub fn try_new_closed(start: T, end: T) -> Result<Self, EmptyGrouping>
pub fn try_new_closed(start: T, end: T) -> Result<Self, EmptyGrouping>
Creates a new closed Willow range, or returns an EmptyGrouping error if the created range would be empty.
use willow_data_model::prelude::*;
assert!(WillowRange::try_new_closed(2, 7).is_ok());
assert!(WillowRange::try_new_closed(7, 2).is_err());
assert!(WillowRange::try_new_closed(2, 2).is_err());Sourcepub fn new_closed(start: T, end: T) -> Self
pub fn new_closed(start: T, end: T) -> Self
Creates a new Willow range from a start value and an optional end value, panicking if the created range would be empty.
use willow_data_model::prelude::*;
let yay = WillowRange::new_closed(2, 7);use willow_data_model::prelude::*;
let nope = WillowRange::new_closed(7, 2);Sourcepub fn includes_value(&self, t: &T) -> bool
pub fn includes_value(&self, t: &T) -> bool
Returns whether the given value is included in self.
use willow_data_model::prelude::*;
let closed = WillowRange::new_closed(2, 7);
assert!(closed.includes_value(&2));
assert!(!closed.includes_value(&1));
assert!(!closed.includes_value(&7));
let open = WillowRange::new_open(2);
assert!(open.includes_value(&2));
assert!(!open.includes_value(&1));
assert!(open.includes_value(&7));Sourcepub fn includes_value_in_intersection(&self, other: &Self, t: &T) -> bool
pub fn includes_value_in_intersection(&self, other: &Self, t: &T) -> bool
Returns whether the given value is included in self and other.
use willow_data_model::prelude::*;
let r1 = WillowRange::new_closed(2, 7);
let r2 = WillowRange::new_open(5);
assert!(r1.includes_value_in_intersection(&r2, &5));
assert!(!r1.includes_value_in_intersection(&r2, &2));
assert!(!r1.includes_value_in_intersection(&r2, &7));
assert!(!r1.includes_value_in_intersection(&r2, &1));
assert!(!r1.includes_value_in_intersection(&r2, &22));Sourcepub fn includes_willow_range(&self, other: &Self) -> bool
pub fn includes_willow_range(&self, other: &Self) -> bool
Returns whether the given Willow range is included in self.
use willow_data_model::prelude::*;
let r = WillowRange::new_closed(2, 7);
assert!(r.includes_willow_range(&WillowRange::new_closed(2, 7)));
assert!(!r.includes_willow_range(&WillowRange::new_closed(1, 7)));
assert!(!r.includes_willow_range(&WillowRange::new_closed(2, 8)));
assert!(!r.includes_willow_range(&WillowRange::new_closed(9, 14)));
assert!(!r.includes_willow_range(&WillowRange::new_open(2)));Sourcepub fn strictly_includes_willow_range(&self, other: &Self) -> bool
pub fn strictly_includes_willow_range(&self, other: &Self) -> bool
Returns whether the given Willow range is strictly included in self.
use willow_data_model::prelude::*;
let r = WillowRange::new_closed(2, 7);
assert!(r.strictly_includes_willow_range(&WillowRange::new_closed(2, 6)));
assert!(r.strictly_includes_willow_range(&WillowRange::new_closed(3, 7)));
assert!(r.strictly_includes_willow_range(&WillowRange::new_closed(3, 6)));
assert!(!r.strictly_includes_willow_range(&WillowRange::new_closed(2, 7)));
assert!(!r.strictly_includes_willow_range(&WillowRange::new_closed(1, 7)));
assert!(!r.strictly_includes_willow_range(&WillowRange::new_closed(2, 8)));
assert!(!r.strictly_includes_willow_range(&WillowRange::new_closed(9, 14)));
assert!(!r.strictly_includes_willow_range(&WillowRange::new_open(2)));Source§impl<T> WillowRange<T>where
T: PartialOrd + Clone,
impl<T> WillowRange<T>where
T: PartialOrd + Clone,
Sourcepub fn intersection_willow_range(
&self,
other: &Self,
) -> Result<Self, EmptyGrouping>
pub fn intersection_willow_range( &self, other: &Self, ) -> Result<Self, EmptyGrouping>
Returns the intersection between self and other, or an EmptyGrouping error if it would be empty.
assert_eq!( WillowRange::new(2, 7).intersection_willow_range(WillowRange::new(5, 9)), Ok(WillowRange::new(5, 7)), ); assert!( WillowRange::new(2, 5).intersection_willow_range(WillowRange::new(7, 9)).is_error(), );
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.
Panicks if T::try_successor returns a valid which is not greater than t.
use willow_data_model::prelude::*;
assert_eq!(WillowRange::<u8>::singleton(5), WillowRange::<u8>::new_closed(5, 6));
assert_eq!(WillowRange::<u8>::singleton(255), WillowRange::<u8>::new_open(255));Source§impl<T> WillowRange<T>where
T: LeastElement,
impl<T> WillowRange<T>where
T: LeastElement,
Trait Implementations§
Source§impl<'a, T> Arbitrary<'a> for WillowRange<T>where
T: Arbitrary<'a> + PartialOrd,
Available on crate feature dev only.
impl<'a, T> Arbitrary<'a> for WillowRange<T>where
T: Arbitrary<'a> + PartialOrd,
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> GreatestElement for WillowRange<T>where
T: LeastElement,
impl<T> GreatestElement for WillowRange<T>where
T: LeastElement,
Source§impl<T: Hash> Hash for WillowRange<T>
impl<T: Hash> Hash for WillowRange<T>
Source§impl<T: PartialEq> PartialEq for WillowRange<T>
impl<T: PartialEq> PartialEq for WillowRange<T>
Source§impl<T> PartialOrd for WillowRange<T>where
T: PartialOrd,
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>where
T: PartialOrd,
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>
impl<T: Copy> Copy for WillowRange<T>
impl<T: Eq> Eq for WillowRange<T>
impl<T> StructuralPartialEq for WillowRange<T>
Auto Trait Implementations§
impl<T> Freeze for WillowRange<T>where
T: Freeze,
impl<T> RefUnwindSafe for WillowRange<T>where
T: RefUnwindSafe,
impl<T> Send for WillowRange<T>where
T: Send,
impl<T> Sync for WillowRange<T>where
T: Sync,
impl<T> Unpin for WillowRange<T>where
T: Unpin,
impl<T> UnwindSafe for WillowRange<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more