WillowRange

Enum WillowRange 

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

Source

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

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

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>

Source

pub fn from_bounds<R>(bounds: &R) -> Self
where 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());
Source

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>

Source

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

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

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

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

pub fn end(&self) -> Option<&T>

Returns a 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(), Some(&8));
assert_eq!(WillowRange::<u8>::from(3..=8).end(), Some(&9));
assert_eq!(WillowRange::<u8>::from(3..).end(), None);
Source

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

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

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,

Source

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>

Source

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,

Source

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

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

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl<T: Clone> Clone for WillowRange<T>

Source§

fn clone(&self) -> WillowRange<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 WillowRange<T>

Source§

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

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

impl<T> From<Range<T>> for WillowRange<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<RangeFrom<T>> for WillowRange<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<RangeFull> for WillowRange<T>
where T: LeastElement,

Source§

fn from(_value: RangeFull) -> Self

Converts to this type from the input type.
Source§

impl<T> From<RangeInclusive<T>> for WillowRange<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<RangeTo<T>> for WillowRange<T>
where T: LeastElement,

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<RangeToInclusive<T>> for WillowRange<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T> GreatestElement for WillowRange<T>

Source§

fn greatest() -> Self

Returns the unique greatest element. Read more
Source§

fn is_greatest(&self) -> bool

Returns true if and only if self is the greatest element.
Source§

impl<T> Hash for WillowRange<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<T> LeastElement for WillowRange<T>

Source§

fn least() -> Self

Returns the unique least element. Read more
Source§

fn is_least(&self) -> bool

Returns true if and only if self is the least element.
Source§

impl<T> LowerSemilattice for WillowRange<T>

Source§

fn greatest_lower_bound(&self, other: &Self) -> Self

Returns the greatest lower bound of 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.

Source§

fn eq(&self, other: &WillowRange<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &WillowRange<T>) -> bool

Tests for !=. 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.

Source§

fn partial_cmp(&self, other: &WillowRange<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> RangeBounds<T> for WillowRange<T>

Source§

fn start_bound(&self) -> Bound<&T>

Start index bound. Read more
Source§

fn end_bound(&self) -> Bound<&T>

End index bound. Read more
1.35.0 · Source§

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

Returns true if item is contained in the range. Read more
Source§

fn is_empty(&self) -> bool
where T: PartialOrd,

🔬This is a nightly-only experimental API. (range_bounds_is_empty)
Returns true if the range contains no items. One-sided ranges (RangeFrom, etc) always return false. Read more
Source§

impl<T> UpperSemilattice for WillowRange<T>

Source§

fn least_upper_bound(&self, other: &Self) -> Self

Returns the least upper bound of self and other, i.e., the unique least element in the type which is greater than or equal to both self and other.
Source§

impl<T> Eq 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> 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> BoundedLowerSemilattice for T

Source§

fn greatest_lower_bound_slice(values: &[Self]) -> Self

Computes the greatest lower bound of all values in the slice. For the empty slice, this is the least element.
Source§

impl<T> BoundedUpperSemilattice for T

Source§

fn least_upper_bound_slice(values: &[Self]) -> Self

Computes the least upper bound of all values in the slice. For the empty slice, this is the greatest element.
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> 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.
Source§

impl<T> BoundedLattice for T

Source§

impl<T> Lattice for T