size_hinter 0.4.2

Iterator adaptors allowing overriding or specifying size_hint.
Documentation
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use core::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

/// Error type for reporting invalid size hints where the size hint would be empty or invalid.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("invalid size hint: values describe an invalid or empty range")]
pub struct InvalidSizeHint;

/// A size hint for an iterator.
///
/// This is an immutable wrapper around the standard iterator size hint tuple
/// `(usize, Option<usize>)`, providing strong gurantees about the bound values
/// (ie. `lower <= upper`), and additional functionality and conversions.
///
/// A size hint describes the range of possible values for the length of an iterator, that is, the
/// number of elements that it will return when enumerated to exhaustion from its current state.
/// While this count does not have to be *exact* (unless the size hint describes an exact bounds,
/// one where the upper and lower bounds are equal) it is an error for the iterator to then produce
/// a number of elements that violates the bounds established by the hint.
///
/// A size hint can never describe an empty range, as 0 is always a valid number of elements
/// remaining for an iterator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[readonly::make]
pub struct SizeHint {
    /// The inclusive lower bound of the size hint.
    pub lower: usize,
    /// The inclusive upper bound of the size hint, or `None` if the upper bound is unbounded.
    pub upper: Option<usize>,
}

impl SizeHint {
    /// A size hint that is always valid, never changes, and conveys no information.
    pub const UNIVERSAL: Self = Self { lower: 0, upper: None };

    /// A size hint that indicates that the iterator will yield no elements.
    pub const ZERO: Self = Self { lower: 0, upper: Some(0) };

    /// Creates a new size hint with the given lower and optional upper bounds.
    ///
    /// # Panics
    ///
    /// Panics if the upper bound is present and less than the lower bound.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// let hint = SizeHint::new(5, Some(10));
    /// assert_eq!(hint.lower, 5);
    /// assert_eq!(hint.upper, Some(10));
    ///```
    #[inline]
    #[must_use]
    pub const fn new(lower: usize, upper: Option<usize>) -> Self {
        match Self::try_new(lower, upper) {
            Ok(hint) => hint,
            Err(_) => panic!("values should describe a valid size hint"),
        }
    }

    /// Tries to create a new size hint with the given lower and optional upper bounds.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidSizeHint`] if the upper bound is present and less than the lower bound.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::{SizeHint, InvalidSizeHint};
    /// # fn main() -> Result<(), InvalidSizeHint> {
    /// let hint = SizeHint::try_new(5, Some(10))?;
    /// assert_eq!(hint.lower, 5);
    /// assert_eq!(hint.upper, Some(10));
    ///
    /// let err: InvalidSizeHint = SizeHint::try_new(10, Some(5)).expect_err("Lower bound is greater than upper bound");
    /// # Ok(())
    /// # }
    ///```
    #[inline]
    pub const fn try_new(lower: usize, upper: Option<usize>) -> Result<Self, InvalidSizeHint> {
        match (lower, upper) {
            (lower, Some(upper)) if lower > upper => Err(InvalidSizeHint),
            _ => Ok(Self { lower, upper }),
        }
    }

    /// Creates a new size hint with the given lower and optional upper bounds.
    ///
    /// # Panics
    ///
    /// Panics if `lower` is greater than `upper`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::{SizeHint, InvalidSizeHint};
    /// let hint = SizeHint::bounded(5, 10);
    /// assert_eq!(hint.lower, 5);
    /// assert_eq!(hint.upper, Some(10));
    ///```
    #[inline]
    #[must_use]
    pub const fn bounded(lower: usize, upper: usize) -> Self {
        match Self::try_bounded(lower, upper) {
            Ok(hint) => hint,
            Err(_) => panic!("values should describe a valid size hint"),
        }
    }

    /// Tries to create a new bounded [`SizeHint`] with the given `lower` and `upper` bounds.
    ///
    /// # Errors
    ///
    /// Returns [`InvalidSizeHint`] if `lower` is greater than `upper`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::{SizeHint, InvalidSizeHint};
    /// # fn main() -> Result<(), InvalidSizeHint> {
    /// let hint = SizeHint::try_bounded(5, 10)?;
    /// assert_eq!(hint.lower, 5);
    /// assert_eq!(hint.upper, Some(10));
    ///
    /// let err: InvalidSizeHint = SizeHint::try_bounded(10, 5).expect_err("SizeHint should be invalid");
    /// # Ok(())
    /// # }
    ///```
    #[inline]
    pub const fn try_bounded(lower: usize, upper: usize) -> Result<Self, InvalidSizeHint> {
        match lower > upper {
            true => Err(InvalidSizeHint),
            false => Ok(Self { lower, upper: Some(upper) }),
        }
    }

    /// Creates a new size hint with the given lower bound and no upper bound.
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// let hint = SizeHint::unbounded(5);
    /// assert_eq!(hint.lower, 5);
    /// assert_eq!(hint.upper, None);
    /// ```
    #[inline]
    #[must_use]
    pub const fn unbounded(lower: usize) -> Self {
        Self { lower, upper: None }
    }

    /// Creates a new size hint with an exact count.
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// let hint = SizeHint::exact(5);
    /// assert_eq!(hint.lower, 5);
    /// assert_eq!(hint.upper, Some(5));
    /// ```
    #[inline]
    #[must_use]
    pub const fn exact(len: usize) -> Self {
        Self { lower: len, upper: Some(len) }
    }

    /// Creates a new size hint with the given upper bound and a lower bound of 0.
    ///
    /// A common use case for this is [`Filter`](core::iter::Filter).
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// let hint = SizeHint::at_most(5);
    /// assert_eq!(hint.lower, 0);
    /// assert_eq!(hint.upper, Some(5));
    /// ```
    #[inline]
    #[must_use]
    pub const fn at_most(upper: usize) -> Self {
        Self { lower: 0, upper: Some(upper) }
    }

    /// Returns the inclusive lower bound of the size hint.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// let hint = SizeHint::new(5, Some(10));
    /// assert_eq!(hint.lower(), 5);
    /// ```
    #[inline]
    #[must_use]
    pub const fn lower(self) -> usize {
        self.lower
    }

    /// Returns the optional inclusive upper bound of the size hint.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// let hint = SizeHint::new(5, Some(10));
    /// assert_eq!(hint.upper(), Some(10));
    /// ```
    #[inline]
    #[must_use]
    pub const fn upper(self) -> Option<usize> {
        self.upper
    }

    /// Returns the size hint as a tuple `(lower, upper)`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// let hint = SizeHint::new(5, Some(10));
    /// assert_eq!(hint.as_hint(), (5, Some(10)));
    /// ```
    #[inline]
    #[must_use]
    pub const fn as_hint(self) -> (usize, Option<usize>) {
        (self.lower, self.upper)
    }

    /// Returns a new [`SizeHint`] with the lower and upper bounds (if present) decremented by 1.
    ///
    /// This is useful for decrementing the size hint of an iterator after it has been advanced.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// let hint = SizeHint::bounded(5, 10);
    /// assert_eq!(hint.decrement(), SizeHint::bounded(4, 9));
    /// ```
    #[inline]
    #[must_use]
    pub fn decrement(self) -> Self {
        Self { lower: self.lower.saturating_sub(1), upper: self.upper.map(|upper| upper.saturating_sub(1)) }
    }

    /// Returns `true` if this size hint range overlaps with another size hint range.
    ///
    /// Two ranges overlap if there exists at least one value that could be contained in both.
    /// This operation is the negation of [`Self::disjoint`], and is also commutative.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// assert!(SizeHint::overlaps(SizeHint::bounded(3, 6), SizeHint::bounded(5, 10)), "should overlap at 5 and 6");
    /// assert!(SizeHint::overlaps(SizeHint::exact(5), SizeHint::UNIVERSAL), "Universal should overlap with any size hint");
    /// assert!(!SizeHint::overlaps(SizeHint::unbounded(11), SizeHint::exact(5)), "should not overlap");
    /// assert!(SizeHint::overlaps(SizeHint::unbounded(11), SizeHint::UNIVERSAL), "two unbounded hints should overlap");
    /// ```
    #[inline]
    #[must_use]
    pub const fn overlaps(self, other: Self) -> bool {
        match (self.as_hint(), other.as_hint()) {
            ((a_lower, Some(a_upper)), (b_lower, Some(b_upper))) => a_lower <= b_upper && b_lower <= a_upper,
            ((_, Some(a_upper)), (b_lower, None)) => a_upper >= b_lower,
            ((a_lower, None), (_, Some(b_upper))) => b_upper >= a_lower,
            ((_, None), (_, None)) => true,
        }
    }

    /// Returns `true` if this size hint range is disjoint with another range.
    ///
    /// Two ranges are disjoint if there exists no value that could be contained in both.
    /// This operation is the negation of [`Self::overlaps`], and is also commutative.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// assert!(SizeHint::disjoint(SizeHint::exact(5), SizeHint::unbounded(10)), "should be disjoint");
    /// assert!(SizeHint::disjoint(SizeHint::exact(5), SizeHint::bounded(6, 10)), "should be disjoint");
    /// assert!(!SizeHint::disjoint(SizeHint::unbounded(11), SizeHint::UNIVERSAL), "two unbounded hints should never be disjoint");
    /// ```
    #[inline]
    #[must_use]
    pub const fn disjoint(self, other: Self) -> bool {
        !Self::overlaps(self, other)
    }

    /// Returns `true` if this size hint range is completely contained within another range.
    ///
    /// This operation is not commutative, i.e. `a.subset_of(b)` does not imply `b.subset_of(a)`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use size_hinter::SizeHint;
    /// assert!(SizeHint::bounded(4, 6).subset_of(SizeHint::bounded(4, 6)), "should be a subset (equal ranges)");
    /// assert!(SizeHint::bounded(4, 6).subset_of(SizeHint::bounded(3, 9)), "should be a subset");
    /// assert!(!SizeHint::bounded(3, 9).subset_of(SizeHint::bounded(4, 6)), "should not be a subset (not commutative)");
    /// assert!(!SizeHint::bounded(2, 6).subset_of(SizeHint::bounded(3, 6)), "should not be a subset (lower bound)");
    /// assert!(!SizeHint::bounded(4, 7).subset_of(SizeHint::bounded(3, 6)), "should not be a subset (upper bound)");
    /// ```
    #[inline]
    #[must_use]
    pub const fn subset_of(self, other: Self) -> bool {
        match (self.as_hint(), other.as_hint()) {
            ((a_low, Some(a_up)), (b_low, Some(b_up))) => a_low >= b_low && a_up <= b_up,
            ((a_low, _), (b_low, None)) => a_low >= b_low,
            ((_, None), (_, Some(_))) => false,
        }
    }
}

impl TryFrom<(usize, Option<usize>)> for SizeHint {
    type Error = InvalidSizeHint;

    #[inline]
    fn try_from(hint: (usize, Option<usize>)) -> Result<Self, Self::Error> {
        match hint {
            (lower, Some(upper)) => Self::try_bounded(lower, upper),
            (lower, None) => Ok(Self::unbounded(lower)),
        }
    }
}

impl From<SizeHint> for (usize, Option<usize>) {
    #[inline]
    fn from(hint: SizeHint) -> Self {
        (hint.lower, hint.upper)
    }
}

impl TryFrom<Range<usize>> for SizeHint {
    type Error = InvalidSizeHint;

    #[inline]
    fn try_from(range: Range<usize>) -> Result<Self, Self::Error> {
        let end = range.end.checked_sub(1).ok_or(InvalidSizeHint)?;
        Self::try_bounded(range.start, end)
    }
}

impl TryFrom<RangeInclusive<usize>> for SizeHint {
    type Error = InvalidSizeHint;

    #[inline]
    fn try_from(range: RangeInclusive<usize>) -> Result<Self, Self::Error> {
        Self::try_bounded(*range.start(), *range.end())
    }
}

impl From<RangeFull> for SizeHint {
    #[inline]
    fn from(_: RangeFull) -> Self {
        Self::UNIVERSAL
    }
}

impl From<RangeFrom<usize>> for SizeHint {
    #[inline]
    fn from(range: RangeFrom<usize>) -> Self {
        Self { lower: range.start, upper: None }
    }
}

impl TryFrom<RangeTo<usize>> for SizeHint {
    type Error = InvalidSizeHint;

    #[inline]
    fn try_from(range: RangeTo<usize>) -> Result<Self, Self::Error> {
        let end = range.end.checked_sub(1).ok_or(InvalidSizeHint)?;
        Self::try_bounded(0, end)
    }
}

impl From<RangeToInclusive<usize>> for SizeHint {
    #[inline]
    fn from(range: RangeToInclusive<usize>) -> Self {
        Self { lower: 0, upper: Some(range.end) }
    }
}

/// A [`SizeHint`] represents a range of possible iterator lengths.
impl RangeBounds<usize> for SizeHint {
    /// Returns the smallest possible iterator length. Always [`Bound::Included`].
    #[inline]
    fn start_bound(&self) -> Bound<&usize> {
        Bound::Included(&self.lower)
    }

    /// Returns the largest possible iterator length. Either [`Bound::Included`] or [`Bound::Unbounded`].
    #[inline]
    fn end_bound(&self) -> Bound<&usize> {
        self.upper.as_ref().map_or(Bound::Unbounded, Bound::Included)
    }
}

impl PartialEq<(usize, Option<usize>)> for SizeHint {
    fn eq(&self, other: &(usize, Option<usize>)) -> bool {
        self.lower == other.0 && self.upper == other.1
    }
}

impl PartialEq<SizeHint> for (usize, Option<usize>) {
    fn eq(&self, other: &SizeHint) -> bool {
        self.0 == other.lower && self.1 == other.upper
    }
}