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
#[derive(Debug, Default, Clone, Copy, PartialOrd, PartialEq)] pub struct Range<T>(pub T, pub T, pub bool); impl<T> Range<T> { pub const fn inclusive(min: T, max: T) -> Self { Self(min, max, false) } pub fn with_extensible(self, extensible: bool) -> Self { let Range(min, max, _) = self; Range(min, max, extensible) } pub const fn min(&self) -> &T { &self.0 } pub const fn max(&self) -> &T { &self.1 } pub const fn extensible(&self) -> bool { self.2 } pub fn wrap_opt(self) -> Range<Option<T>> { let Range(min, max, extensible) = self; Range(Some(min), Some(max), extensible) } } impl<T> Range<Option<T>> { pub fn none() -> Self { Range(None, None, false) } pub fn min_max(&self, min_fn: impl Fn() -> T, max_fn: impl Fn() -> T) -> Option<(T, T)> where T: Copy, { match (self.0, self.1) { (Some(min), Some(max)) => Some((min, max)), (Some(min), None) => Some((min, max_fn())), (None, Some(max)) => Some((min_fn(), max)), (None, None) => None, } } } impl Range<Option<i64>> {}