mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-04 02:54:00 +00:00
Implement SizeHint trait for BufReader, Emtpy, and Chain
This commit is contained in:
parent
c8e0f8aaa3
commit
96255f82c9
@ -1,6 +1,6 @@
|
||||
use crate::cmp;
|
||||
use crate::fmt;
|
||||
use crate::io::{self, BufRead, Initializer, IoSliceMut, Read, Seek, SeekFrom, DEFAULT_BUF_SIZE};
|
||||
use crate::io::{self, BufRead, Initializer, IoSliceMut, Read, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE};
|
||||
|
||||
/// The `BufReader<R>` struct adds buffering to any reader.
|
||||
///
|
||||
@ -435,3 +435,10 @@ impl<R: Seek> Seek for BufReader<R> {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> SizeHint for BufReader<T> {
|
||||
fn lower_bound(&self) -> usize {
|
||||
self.buffer().len()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2239,6 +2239,20 @@ impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> SizeHint for Chain<T, U> {
|
||||
fn lower_bound(&self) -> usize {
|
||||
SizeHint::lower_bound(&self.first) + SizeHint::lower_bound(&self.second)
|
||||
}
|
||||
|
||||
fn upper_bound(&self) -> Option<usize > {
|
||||
match (SizeHint::upper_bound(&self.first), SizeHint::upper_bound(&self.second)) {
|
||||
(Some(first), Some(second)) => Some(first + second),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Reader adaptor which limits the bytes read from an underlying reader.
|
||||
///
|
||||
/// This struct is generally created by calling [`take`] on a reader.
|
||||
@ -2491,12 +2505,6 @@ impl<T> SizeHint for T {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> SizeHint for BufReader<T> {
|
||||
fn lower_bound(&self) -> usize {
|
||||
self.buffer().len()
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over the contents of an instance of `BufRead` split on a
|
||||
/// particular byte.
|
||||
///
|
||||
|
@ -4,7 +4,7 @@
|
||||
mod tests;
|
||||
|
||||
use crate::fmt;
|
||||
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
|
||||
use crate::io::{self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write};
|
||||
|
||||
/// A reader which is always at EOF.
|
||||
///
|
||||
@ -80,6 +80,12 @@ impl fmt::Debug for Empty {
|
||||
}
|
||||
}
|
||||
|
||||
impl SizeHint for Empty {
|
||||
fn upper_bound(&self) -> Option<usize> {
|
||||
Some(0)
|
||||
}
|
||||
}
|
||||
|
||||
/// A reader which yields one byte over and over and over and over and over and...
|
||||
///
|
||||
/// This struct is generally created by calling [`repeat()`]. Please
|
||||
|
Loading…
Reference in New Issue
Block a user