| 1 | //! Arithmetic on `Iterator.size_hint()` values. |
| 2 | //! |
| 3 | |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/; |
| 5 | |
| 6 | /// `SizeHint` is the return type of `Iterator::size_hint()`. |
| 7 | pub type SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# = (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>); |
| 8 | |
| 9 | /// Add `SizeHint` correctly. |
| 10 | #[inline] |
| 11 | pub fn addrust-analyzer cargo itertools 0.14.0 size_hint/add().(a: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#, b: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 12 | let min = a.0.saturating_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_add().(b.0); |
| 13 | let max = match (a.1, b.1) { |
| 14 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(x), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(y)) => x.checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_add().(y), |
| 15 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 16 | }; |
| 17 | |
| 18 | (min, max) |
| 19 | } |
| 20 | |
| 21 | /// Add `x` correctly to a `SizeHint`. |
| 22 | #[inline] |
| 23 | pub fn add_scalarrust-analyzer cargo itertools 0.14.0 size_hint/add_scalar().(sh: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#, x: usize) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 24 | let (mut low, mut hi) = sh; |
| 25 | low = low.saturating_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_add().(x); |
| 26 | hi = hi.and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|elt| elt.checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_add().(x)); |
| 27 | (low, hi) |
| 28 | } |
| 29 | |
| 30 | /// Subtract `x` correctly from a `SizeHint`. |
| 31 | #[inline] |
| 32 | pub fn sub_scalarrust-analyzer cargo itertools 0.14.0 size_hint/sub_scalar().(sh: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#, x: usize) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 33 | let (mut low, mut hi) = sh; |
| 34 | low = low.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(x); |
| 35 | hi = hi.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|elt| elt.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(x)); |
| 36 | (low, hi) |
| 37 | } |
| 38 | |
| 39 | /// Multiply `SizeHint` correctly |
| 40 | #[inline] |
| 41 | pub fn mulrust-analyzer cargo itertools 0.14.0 size_hint/mul().(a: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#, b: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 42 | let low = a.0.saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(b.0); |
| 43 | let hi = match (a.1, b.1) { |
| 44 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(x), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(y)) => x.checked_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_mul().(y), |
| 45 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) | (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0)) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0), |
| 46 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 47 | }; |
| 48 | (low, hi) |
| 49 | } |
| 50 | |
| 51 | /// Multiply `x` correctly with a `SizeHint`. |
| 52 | #[inline] |
| 53 | pub fn mul_scalarrust-analyzer cargo itertools 0.14.0 size_hint/mul_scalar().(sh: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#, x: usize) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 54 | let (mut low, mut hi) = sh; |
| 55 | low = low.saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(x); |
| 56 | hi = hi.and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|elt| elt.checked_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]checked_mul().(x)); |
| 57 | (low, hi) |
| 58 | } |
| 59 | |
| 60 | /// Return the maximum |
| 61 | #[inline] |
| 62 | pub fn maxrust-analyzer cargo itertools 0.14.0 size_hint/max().(a: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#, b: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 63 | let (a_lower, a_upper) = a; |
| 64 | let (b_lower, b_upper) = b; |
| 65 | |
| 66 | let lower = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(a_lower, b_lower); |
| 67 | |
| 68 | let upper = match (a_upper, b_upper) { |
| 69 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(x), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(y)) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(x, y)), |
| 70 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 71 | }; |
| 72 | |
| 73 | (lower, upper) |
| 74 | } |
| 75 | |
| 76 | /// Return the minimum |
| 77 | #[inline] |
| 78 | pub fn minrust-analyzer cargo itertools 0.14.0 size_hint/min().(a: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#, b: SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint#) -> SizeHintrust-analyzer cargo itertools 0.14.0 size_hint/SizeHint# { |
| 79 | let (a_lower, a_upper) = a; |
| 80 | let (b_lower, b_upper) = b; |
| 81 | let lower = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(a_lower, b_lower); |
| 82 | let upper = match (a_upper, b_upper) { |
| 83 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(u1), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(u2)) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(u1, u2)), |
| 84 | _ => a_upper.orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or().(b_upper), |
| 85 | }; |
| 86 | (lower, upper) |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn mul_size_hints() { |
| 91 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(mul((3, Some(4)), (3, Some(4))), (9, Some(16))); |
| 92 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(mul((3, Some(4)), (usize::MAX, None)), (usize::MAX, None)); |
| 93 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(mul((3, None), (0, Some(0))), (0, Some(0))); |
| 94 | } |