Skip to content

Commit 2c683ff

Browse files
committed
Implement SizeHint trait for BufReader, Emtpy, and Chain
1 parent 6040a52 commit 2c683ff

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

library/std/src/io/buffered/bufreader.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cmp;
22
use crate::fmt;
3-
use crate::io::{self, BufRead, Initializer, IoSliceMut, Read, Seek, SeekFrom, DEFAULT_BUF_SIZE};
3+
use crate::io::{self, BufRead, Initializer, IoSliceMut, Read, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE};
44

55
/// The `BufReader<R>` struct adds buffering to any reader.
66
///
@@ -422,3 +422,10 @@ impl<R: Seek> Seek for BufReader<R> {
422422
})
423423
}
424424
}
425+
426+
impl<T> SizeHint for BufReader<T> {
427+
fn lower_bound(&self) -> usize {
428+
self.buffer().len()
429+
}
430+
}
431+

library/std/src/io/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,20 @@ impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
22362236
}
22372237
}
22382238

2239+
impl<T, U> SizeHint for Chain<T, U> {
2240+
fn lower_bound(&self) -> usize {
2241+
SizeHint::lower_bound(&self.first) + SizeHint::lower_bound(&self.second)
2242+
}
2243+
2244+
fn upper_bound(&self) -> Option<usize > {
2245+
match (SizeHint::upper_bound(&self.first), SizeHint::upper_bound(&self.second)) {
2246+
(Some(first), Some(second)) => Some(first + second),
2247+
_ => None,
2248+
}
2249+
}
2250+
}
2251+
2252+
22392253
/// Reader adaptor which limits the bytes read from an underlying reader.
22402254
///
22412255
/// This struct is generally created by calling [`take`] on a reader.
@@ -2488,12 +2502,6 @@ impl<T> SizeHint for T {
24882502
}
24892503
}
24902504

2491-
impl<T> SizeHint for BufReader<T> {
2492-
fn lower_bound(&self) -> usize {
2493-
self.buffer().len()
2494-
}
2495-
}
2496-
24972505
/// An iterator over the contents of an instance of `BufRead` split on a
24982506
/// particular byte.
24992507
///

library/std/src/io/util.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
mod tests;
55

66
use crate::fmt;
7-
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Write};
7+
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, SizeHint, Write};
88

99
/// A reader which is always at EOF.
1010
///
@@ -65,6 +65,12 @@ impl fmt::Debug for Empty {
6565
}
6666
}
6767

68+
impl SizeHint for Empty {
69+
fn upper_bound(&self) -> Option<usize> {
70+
Some(0)
71+
}
72+
}
73+
6874
/// A reader which yields one byte over and over and over and over and over and...
6975
///
7076
/// This struct is generally created by calling [`repeat()`]. Please

0 commit comments

Comments
 (0)