-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add read_all to std:io::Read to parallel write_all in std::io::Write. #23369
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,6 +187,32 @@ pub trait Read { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize>; | ||
|
||
/// Read as many bytes as buf can hold, stopping at EOF or on error | ||
/// | ||
/// This method will continuously call `read` while there is more data to | ||
/// read. This method will not return until the entire buffer has been | ||
/// successfully read or an error occurs. The first error generated from | ||
/// this method will be returned. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return the first error that `read` returns. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn read_all(&mut self, mut buf: &mut [u8]) -> Result<()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should give a warning (#25049). |
||
let mut total = 0; | ||
while total < buf.len() { | ||
match self.read(&mut buf[total..]) { | ||
Ok(0) => return Err(Error::new(ErrorKind::ShortRead(total), | ||
"failed to read whole buffer", | ||
None)), | ||
Ok(n) => total += n, | ||
Err(ref e) if e.kind() == ErrorKind::Interrupted => {} | ||
Err(e) => return Err(e), | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Read all bytes until EOF in this source, placing them into `buf`. | ||
/// | ||
/// All bytes read from this source will be appended to the specified buffer | ||
|
@@ -989,6 +1015,21 @@ mod tests { | |
assert_eq!(s.next(), None); | ||
} | ||
|
||
#[test] | ||
fn read_all() { | ||
let mut c = Cursor::new(b"hello"); | ||
let mut v = [0 as u8; 5]; | ||
assert_eq!(c.read_all(&mut v), Ok()); | ||
assert_eq!(v, b"hello"); | ||
|
||
let mut c = Cursor::new(b"short"); | ||
let mut v = [115 as u8; 6]; | ||
assert_eq!(c.read_all(&mut v), Error::new(ErrorKind::ShortRead(5), | ||
"failed to read whole buffer", | ||
None)); | ||
assert_eq!(v, b"shorts"); | ||
} | ||
|
||
#[test] | ||
fn read_to_end() { | ||
let mut c = Cursor::new(&b""[..]); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the size of the enum dramatically.