ClosedRange

Struct ClosedRange 

Source
pub struct ClosedRange<T> { /* private fields */ }
Expand description

A non-empty closed range with an inclusive start value and a strictly-greater exclusive end value.

It includes all values that are greater than or equal to the start value and strictly less than the end value.

use willow_data_model::prelude::*;

let r = ClosedRange::new(2, 7);
assert_eq!(r.start(), &2);
assert_eq!(r.end(), &7);

assert!(ClosedRange::try_new(7, 2).is_err());
assert!(ClosedRange::try_new(2, 2).is_err());

Implementations§

Source§

impl<T> ClosedRange<T>
where T: PartialOrd,

Source

pub fn try_new(start: T, end: T) -> Result<Self, EmptyGrouping>

Creates a new closed range, or an EmptyGrouping error if start >= end.

use willow_data_model::prelude::*;

assert!(ClosedRange::try_new(2, 7).is_ok());
assert!(ClosedRange::try_new(7, 2).is_err());
assert!(ClosedRange::try_new(2, 2).is_err());
Source

pub fn new(start: T, end: T) -> Self

Creates a new closed range, panicking if it would be empty.

use willow_data_model::prelude::*;

let yay = ClosedRange::new(2, 7);
use willow_data_model::prelude::*;

let nope = ClosedRange::new(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 r = ClosedRange::new(2, 7);
assert!(r.includes_value(&2));
assert!(!r.includes_value(&1));
assert!(!r.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 = ClosedRange::new(2, 7);
let r2 = ClosedRange::new(5, 9);
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, &22));
Source

pub fn includes_closed_range(&self, other: &Self) -> bool

Returns whether the given closed range is included in self.

use willow_data_model::prelude::*;

let r = ClosedRange::new(2, 7);
assert!(r.includes_closed_range(&ClosedRange::new(3, 6)));
assert!(r.includes_closed_range(&ClosedRange::new(2, 5)));
assert!(r.includes_closed_range(&ClosedRange::new(3, 7)));
assert!(r.includes_closed_range(&ClosedRange::new(2, 7)));
assert!(!r.includes_closed_range(&ClosedRange::new(1, 7)));
assert!(!r.includes_closed_range(&ClosedRange::new(2, 8)));
assert!(!r.includes_closed_range(&ClosedRange::new(9, 14)));
Source

pub fn strictly_includes_closed_range(&self, other: &Self) -> bool

Returns whether the given closed range is strictly included in self.

use willow_data_model::prelude::*;

let r = ClosedRange::new(2, 7);
assert!(r.strictly_includes_closed_range(&ClosedRange::new(2, 6)));
assert!(r.strictly_includes_closed_range(&ClosedRange::new(3, 7)));
assert!(r.strictly_includes_closed_range(&ClosedRange::new(3, 6)));
assert!(!r.strictly_includes_closed_range(&ClosedRange::new(2, 7)));
assert!(!r.strictly_includes_closed_range(&ClosedRange::new(1, 7)));
assert!(!r.strictly_includes_closed_range(&ClosedRange::new(2, 8)));
assert!(!r.strictly_includes_closed_range(&ClosedRange::new(9, 14)));
Source§

impl<T> ClosedRange<T>
where T: PartialOrd + Clone,

Source

pub fn intersection_closed_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!( ClosedRange::new(2, 7).intersection_closed_range(ClosedRange::new(5, 9)), Ok(ClosedRange::new(5, 7)), ); assert!( ClosedRange::new(2, 5).intersection_closed_range(ClosedRange::new(7, 9)).is_error(), );

Source§

impl<T> ClosedRange<T>

Source

pub unsafe fn new_unchecked(start: T, end: T) -> Self

Creates a new closed range, without enforcing non-emptyness.

§Safety

Calling this method with start >= end is undefined behaviour.

use willow_data_model::prelude::*;

let yay = unsafe { ClosedRange::new_unchecked(2, 7) };
Source

pub fn start(&self) -> &T

Returns a reference to the start value of this closed range.

use willow_data_model::prelude::*;

let r = ClosedRange::new(2, 7);
assert_eq!(r.start(), &2);
Source

pub fn end(&self) -> &T

Returns a reference to the end value of this closed range.

use willow_data_model::prelude::*;

let r = ClosedRange::new(2, 7);
assert_eq!(r.end(), &7);
Source

pub fn into_parts(self) -> (T, T)

Takes ownership of a closed range and returns its start and end value.

use willow_data_model::prelude::*;

let r = ClosedRange::new(2, 7);
assert_eq!(r.into_parts(), (2, 7));

Trait Implementations§

Source§

impl<'a, T> Arbitrary<'a> for ClosedRange<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 ClosedRange<T>

Source§

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

Source§

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

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

impl<T: Hash> Hash for ClosedRange<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 ClosedRange<T>

Source§

fn eq(&self, other: &ClosedRange<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 ClosedRange<T>
where T: PartialOrd,

Source§

fn partial_cmp(&self, other: &Self) -> 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 ClosedRange<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 ClosedRange<T>

Source§

impl<T: Eq> Eq for ClosedRange<T>

Source§

impl<T> StructuralPartialEq for ClosedRange<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for ClosedRange<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.