WillowRange

Enum WillowRange 

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

Source

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

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

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

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

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

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

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

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

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),
);
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, and if the resulting range is non-empty.

Source

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

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,

Source

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

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

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

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

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

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

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

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,

Source

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>

Source

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,

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(), WillowRange::<u8>::new_open(0));
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>::new_open(1).is_full(), false);

Trait Implementations§

Source§

impl<'a, T> Arbitrary<'a> for WillowRange<T>
where T: Arbitrary<'a> + PartialOrd,

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> GreatestElement for WillowRange<T>
where T: LeastElement,

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> 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: PartialEq> PartialEq for WillowRange<T>

Source§

fn eq(&self, other: &WillowRange<T>) -> 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> 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§

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: Copy> Copy for WillowRange<T>

Source§

impl<T: Eq> Eq for WillowRange<T>

Source§

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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