Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defactored Bytes::read #56363

Merged
merged 4 commits into from
Dec 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@

use cmp;
use fmt;
use slice;
use str;
use memchr;
use ptr;
Expand Down Expand Up @@ -1936,18 +1937,6 @@ impl<T: BufRead> BufRead for Take<T> {
}
}

fn read_one_byte(reader: &mut dyn Read) -> Option<Result<u8>> {
let mut buf = [0];
loop {
return match reader.read(&mut buf) {
Ok(0) => None,
Ok(..) => Some(Ok(buf[0])),
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => Some(Err(e)),
};
}
}

/// An iterator over `u8` values of a reader.
///
/// This struct is generally created by calling [`bytes`] on a reader.
Expand All @@ -1965,7 +1954,15 @@ impl<R: Read> Iterator for Bytes<R> {
type Item = Result<u8>;

fn next(&mut self) -> Option<Result<u8>> {
read_one_byte(&mut self.inner)
let mut byte = 0;
loop {
return match self.inner.read(slice::from_mut(&mut byte)) {
Ok(0) => None,
Ok(..) => Some(Ok(byte)),
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => Some(Err(e)),
Lucretiel marked this conversation as resolved.
Show resolved Hide resolved
};
}
}
}

Expand Down