1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
use crate::{Bound, UpperBound};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// A range only bounded above (either inclusive or exclusive).
///
/// Generalizes over [`std::ops::RangeTo`] and [`std::ops::RangeToInclusive`].
///
/// While an `UpperoundedRange` can be constructed directly, it will most likely
/// result from one or more range operations.
/// ```
/// use rangetools::{Rangetools, UpperBound, UpperBoundedRange};
///
/// let i = (..5).intersection(..=3);
/// assert_eq!(i, UpperBoundedRange { end: UpperBound::included(3) });
/// ```
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct UpperBoundedRange<T> {
/// The upper bound of the range (can be inclusive or exclusive).
pub end: UpperBound<T>,
}
impl<T> From<std::ops::RangeTo<T>> for UpperBoundedRange<T> {
fn from(r: std::ops::RangeTo<T>) -> Self {
Self {
end: UpperBound::excluded(r.end),
}
}
}
impl<T> From<std::ops::RangeToInclusive<T>> for UpperBoundedRange<T> {
fn from(r: std::ops::RangeToInclusive<T>) -> Self {
Self {
end: UpperBound::included(r.end),
}
}
}
impl<T: Copy + Ord> UpperBoundedRange<T> {
/// Constructs a new `UpperBoundedRange` from an upper bound.
///
/// # Example
/// ```
/// use rangetools::{UpperBound, UpperBoundedRange};
///
/// let r = UpperBoundedRange::new(UpperBound::included(10));
/// assert!(r.contains(5));
/// ```
pub fn new(end: UpperBound<T>) -> Self {
Self { end }
}
/// Returns true if the range contains `t`.
///
/// # Example
/// ```
/// use rangetools::Rangetools;
///
/// let i = (..5).intersection(..10);
/// assert!(i.contains(4));
/// assert!(!i.contains(9));
/// ```
pub fn contains(&self, t: T) -> bool {
match self.end.0 {
Bound::Excluded(x) => t < x,
Bound::Included(i) => t <= i,
}
}
}