|
252 | 252 | mod tests; |
253 | 253 |
|
254 | 254 | use crate::cmp; |
| 255 | +use crate::convert::TryInto; |
255 | 256 | use crate::fmt; |
256 | 257 | use crate::ops::{Deref, DerefMut}; |
257 | 258 | use crate::ptr; |
@@ -2291,13 +2292,15 @@ impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> { |
2291 | 2292 | } |
2292 | 2293 |
|
2293 | 2294 | impl<T, U> SizeHint for Chain<T, U> { |
| 2295 | + #[inline] |
2294 | 2296 | fn lower_bound(&self) -> usize { |
2295 | 2297 | SizeHint::lower_bound(&self.first) + SizeHint::lower_bound(&self.second) |
2296 | 2298 | } |
2297 | 2299 |
|
| 2300 | + #[inline] |
2298 | 2301 | fn upper_bound(&self) -> Option<usize> { |
2299 | 2302 | match (SizeHint::upper_bound(&self.first), SizeHint::upper_bound(&self.second)) { |
2300 | | - (Some(first), Some(second)) => Some(first + second), |
| 2303 | + (Some(first), Some(second)) => first.checked_add(second), |
2301 | 2304 | _ => None, |
2302 | 2305 | } |
2303 | 2306 | } |
@@ -2502,6 +2505,21 @@ impl<T: BufRead> BufRead for Take<T> { |
2502 | 2505 | } |
2503 | 2506 | } |
2504 | 2507 |
|
| 2508 | +impl<T> SizeHint for Take<T> { |
| 2509 | + #[inline] |
| 2510 | + fn lower_bound(&self) -> usize { |
| 2511 | + cmp::min(SizeHint::lower_bound(&self.inner) as u64, self.limit) as usize |
| 2512 | + } |
| 2513 | + |
| 2514 | + #[inline] |
| 2515 | + fn upper_bound(&self) -> Option<usize> { |
| 2516 | + match SizeHint::upper_bound(&self.inner) { |
| 2517 | + Some(upper_bound) => Some(cmp::min(upper_bound as u64, self.limit) as usize), |
| 2518 | + None => self.limit.try_into().ok(), |
| 2519 | + } |
| 2520 | + } |
| 2521 | +} |
| 2522 | + |
2505 | 2523 | /// An iterator over `u8` values of a reader. |
2506 | 2524 | /// |
2507 | 2525 | /// This struct is generally created by calling [`bytes`] on a reader. |
@@ -2546,15 +2564,53 @@ trait SizeHint { |
2546 | 2564 | } |
2547 | 2565 |
|
2548 | 2566 | impl<T> SizeHint for T { |
| 2567 | + #[inline] |
2549 | 2568 | default fn lower_bound(&self) -> usize { |
2550 | 2569 | 0 |
2551 | 2570 | } |
2552 | 2571 |
|
| 2572 | + #[inline] |
2553 | 2573 | default fn upper_bound(&self) -> Option<usize> { |
2554 | 2574 | None |
2555 | 2575 | } |
2556 | 2576 | } |
2557 | 2577 |
|
| 2578 | +impl<T> SizeHint for &mut T { |
| 2579 | + #[inline] |
| 2580 | + fn lower_bound(&self) -> usize { |
| 2581 | + SizeHint::lower_bound(*self) |
| 2582 | + } |
| 2583 | + |
| 2584 | + #[inline] |
| 2585 | + fn upper_bound(&self) -> Option<usize> { |
| 2586 | + SizeHint::upper_bound(*self) |
| 2587 | + } |
| 2588 | +} |
| 2589 | + |
| 2590 | +impl<T> SizeHint for Box<T> { |
| 2591 | + #[inline] |
| 2592 | + fn lower_bound(&self) -> usize { |
| 2593 | + SizeHint::lower_bound(&**self) |
| 2594 | + } |
| 2595 | + |
| 2596 | + #[inline] |
| 2597 | + fn upper_bound(&self) -> Option<usize> { |
| 2598 | + SizeHint::upper_bound(&**self) |
| 2599 | + } |
| 2600 | +} |
| 2601 | + |
| 2602 | +impl SizeHint for &[u8] { |
| 2603 | + #[inline] |
| 2604 | + fn lower_bound(&self) -> usize { |
| 2605 | + self.len() |
| 2606 | + } |
| 2607 | + |
| 2608 | + #[inline] |
| 2609 | + fn upper_bound(&self) -> Option<usize> { |
| 2610 | + Some(self.len()) |
| 2611 | + } |
| 2612 | +} |
| 2613 | + |
2558 | 2614 | /// An iterator over the contents of an instance of `BufRead` split on a |
2559 | 2615 | /// particular byte. |
2560 | 2616 | /// |
|
0 commit comments