1use core::{
4 cmp, fmt,
5 ops::{Bound, RangeBounds},
6};
7
8#[derive(Clone, Default, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
11#[cfg_attr(feature = "strict", repr(C))]
12pub struct ArchivedRange<T> {
13 pub start: T,
15 pub end: T,
17}
18
19impl<T: fmt::Debug> fmt::Debug for ArchivedRange<T> {
20 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
21 self.start.fmt(fmt)?;
22 write!(fmt, "..")?;
23 self.end.fmt(fmt)?;
24 Ok(())
25 }
26}
27
28impl<T: PartialOrd<T>> ArchivedRange<T> {
29 #[inline]
31 pub fn contains<U>(&self, item: &U) -> bool
32 where
33 T: PartialOrd<U>,
34 U: PartialOrd<T> + ?Sized,
35 {
36 <Self as RangeBounds<T>>::contains(self, item)
37 }
38
39 #[inline]
41 pub fn is_empty(&self) -> bool {
42 match self.start.partial_cmp(&self.end) {
43 None | Some(cmp::Ordering::Greater) | Some(cmp::Ordering::Equal) => true,
44 Some(cmp::Ordering::Less) => false,
45 }
46 }
47}
48
49impl<T> RangeBounds<T> for ArchivedRange<T> {
50 #[inline]
51 fn start_bound(&self) -> Bound<&T> {
52 Bound::Included(&self.start)
53 }
54
55 #[inline]
56 fn end_bound(&self) -> Bound<&T> {
57 Bound::Excluded(&self.end)
58 }
59}
60
61#[derive(Clone, Default, PartialEq, Eq, Hash)]
65#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
66#[cfg_attr(feature = "strict", repr(C))]
67pub struct ArchivedRangeInclusive<T> {
68 pub start: T,
70 pub end: T,
72}
73
74impl<T: fmt::Debug> fmt::Debug for ArchivedRangeInclusive<T> {
75 #[inline]
76 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
77 self.start.fmt(fmt)?;
78 write!(fmt, "..=")?;
79 self.end.fmt(fmt)?;
80 Ok(())
81 }
82}
83
84impl<T: PartialOrd<T>> ArchivedRangeInclusive<T> {
85 #[inline]
87 pub fn contains<U>(&self, item: &U) -> bool
88 where
89 T: PartialOrd<U>,
90 U: PartialOrd<T> + ?Sized,
91 {
92 <Self as RangeBounds<T>>::contains(self, item)
93 }
94
95 #[inline]
97 pub fn is_empty(&self) -> bool {
98 match self.start.partial_cmp(&self.end) {
99 None | Some(cmp::Ordering::Greater) => true,
100 Some(cmp::Ordering::Less) | Some(cmp::Ordering::Equal) => false,
101 }
102 }
103}
104
105impl<T> RangeBounds<T> for ArchivedRangeInclusive<T> {
106 #[inline]
107 fn start_bound(&self) -> Bound<&T> {
108 Bound::Included(&self.start)
109 }
110
111 #[inline]
112 fn end_bound(&self) -> Bound<&T> {
113 Bound::Included(&self.end)
114 }
115}
116
117#[derive(Clone, Default, PartialEq, Eq, Hash)]
119#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
120#[cfg_attr(feature = "strict", repr(C))]
121pub struct ArchivedRangeFrom<T> {
122 pub start: T,
124}
125
126impl<T: fmt::Debug> fmt::Debug for ArchivedRangeFrom<T> {
127 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
128 self.start.fmt(fmt)?;
129 write!(fmt, "..")?;
130 Ok(())
131 }
132}
133
134impl<T: PartialOrd<T>> ArchivedRangeFrom<T> {
135 #[inline]
137 pub fn contains<U>(&self, item: &U) -> bool
138 where
139 T: PartialOrd<U>,
140 U: ?Sized + PartialOrd<T>,
141 {
142 <Self as RangeBounds<T>>::contains(self, item)
143 }
144}
145
146impl<T> RangeBounds<T> for ArchivedRangeFrom<T> {
147 #[inline]
148 fn start_bound(&self) -> Bound<&T> {
149 Bound::Included(&self.start)
150 }
151
152 #[inline]
153 fn end_bound(&self) -> Bound<&T> {
154 Bound::Unbounded
155 }
156}
157
158#[derive(Clone, Default, PartialEq, Eq, Hash)]
160#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
161#[cfg_attr(feature = "strict", repr(C))]
162pub struct ArchivedRangeTo<T> {
163 pub end: T,
165}
166
167impl<T: fmt::Debug> fmt::Debug for ArchivedRangeTo<T> {
168 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
169 write!(fmt, "..")?;
170 self.end.fmt(fmt)?;
171 Ok(())
172 }
173}
174
175impl<T: PartialOrd<T>> ArchivedRangeTo<T> {
176 #[inline]
178 pub fn contains<U>(&self, item: &U) -> bool
179 where
180 T: PartialOrd<U>,
181 U: ?Sized + PartialOrd<T>,
182 {
183 <Self as RangeBounds<T>>::contains(self, item)
184 }
185}
186
187impl<T> RangeBounds<T> for ArchivedRangeTo<T> {
188 #[inline]
189 fn start_bound(&self) -> Bound<&T> {
190 Bound::Unbounded
191 }
192
193 #[inline]
194 fn end_bound(&self) -> Bound<&T> {
195 Bound::Excluded(&self.end)
196 }
197}
198
199#[derive(Clone, Default, PartialEq, Eq, Hash)]
201#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
202#[cfg_attr(feature = "strict", repr(C))]
203pub struct ArchivedRangeToInclusive<T> {
204 pub end: T,
206}
207
208impl<T: fmt::Debug> fmt::Debug for ArchivedRangeToInclusive<T> {
209 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
210 write!(fmt, "..=")?;
211 self.end.fmt(fmt)?;
212 Ok(())
213 }
214}
215
216impl<T: PartialOrd<T>> ArchivedRangeToInclusive<T> {
217 #[inline]
219 pub fn contains<U>(&self, item: &U) -> bool
220 where
221 T: PartialOrd<U>,
222 U: ?Sized + PartialOrd<T>,
223 {
224 <Self as RangeBounds<T>>::contains(self, item)
225 }
226}
227
228impl<T> RangeBounds<T> for ArchivedRangeToInclusive<T> {
229 #[inline]
230 fn start_bound(&self) -> Bound<&T> {
231 Bound::Unbounded
232 }
233
234 #[inline]
235 fn end_bound(&self) -> Bound<&T> {
236 Bound::Included(&self.end)
237 }
238}