Skip to content

Commit 96255f8

Browse files
committed
Implement SizeHint trait for BufReader, Emtpy, and Chain
1 parent c8e0f8a commit 96255f8

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
///
@@ -435,3 +435,10 @@ impl<R: Seek> Seek for BufReader<R> {
435435
})
436436
}
437437
}
438+
439+
impl<T> SizeHint for BufReader<T> {
440+
fn lower_bound(&self) -> usize {
441+
self.buffer().len()
442+
}
443+
}
444+

library/std/src/io/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,20 @@ impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
22392239
}
22402240
}
22412241

2242+
impl<T, U> SizeHint for Chain<T, U> {
2243+
fn lower_bound(&self) -> usize {
2244+
SizeHint::lower_bound(&self.first) + SizeHint::lower_bound(&self.second)
2245+
}
2246+
2247+
fn upper_bound(&self) -> Option<usize > {
2248+
match (SizeHint::upper_bound(&self.first), SizeHint::upper_bound(&self.second)) {
2249+
(Some(first), Some(second)) => Some(first + second),
2250+
_ => None,
2251+
}
2252+
}
2253+
}
2254+
2255+
22422256
/// Reader adaptor which limits the bytes read from an underlying reader.
22432257
///
22442258
/// This struct is generally created by calling [`take`] on a reader.
@@ -2491,12 +2505,6 @@ impl<T> SizeHint for T {
24912505
}
24922506
}
24932507

2494-
impl<T> SizeHint for BufReader<T> {
2495-
fn lower_bound(&self) -> usize {
2496-
self.buffer().len()
2497-
}
2498-
}
2499-
25002508
/// An iterator over the contents of an instance of `BufRead` split on a
25012509
/// particular byte.
25022510
///

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, Seek, SeekFrom, Write};
7+
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write};
88

99
/// A reader which is always at EOF.
1010
///
@@ -80,6 +80,12 @@ impl fmt::Debug for Empty {
8080
}
8181
}
8282

83+
impl SizeHint for Empty {
84+
fn upper_bound(&self) -> Option<usize> {
85+
Some(0)
86+
}
87+
}
88+
8389
/// A reader which yields one byte over and over and over and over and over and...
8490
///
8591
/// This struct is generally created by calling [`repeat()`]. Please

0 commit comments

Comments
 (0)