-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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 Reader impl for &[u8] and Writer impl for &mut [u8] #18980
Conversation
Needs a rebase. |
Rebased, and ready for a re-review. |
30fdb60
to
b1a1688
Compare
let write_len = buf.len(); | ||
if write_len > self.len() { | ||
return Err(IoError { | ||
kind: io::OtherIoError, |
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.
couldn't this be a ShortWrite?
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.
How does an EndOfFile
if self.len()
is 0
, and ShortWrite
if it was able to partially right the file sound?
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.
Sounds great :)
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.
Should writing a &[]
to a &[]
be an EndOfFile, or be okay?
The let mut buf = vec![];
write!(&mut buf, "hello world"); // Calls Vec's impl
write(buf, "hello world"); // Calls &mut [u8]'s impl |
@sfackler: That is... weird. I would have expected |
@erickt it's due to this line, which was apparently added to force people to pass references in like you would a function: https://github.com/rust-lang/rust/blob/master/src/libstd/macros.rs#L266 |
While it's possible to implement Other than that this looks great though, thanks @erickt! |
I've updated my PR to factor out the |
@@ -447,12 +447,11 @@ impl<T, E> Result<T, E> { | |||
/// use std::io::{BufReader, IoResult}; | |||
/// | |||
/// let buffer = "1\n2\n3\n4\n"; |
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.
Shouldn't this be let mut buffer = b"...";
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.
Nice catch.
Looks great, thanks @erickt! I think there may be a waiting failure in the doctest, but otherwise r=me |
@@ -446,13 +446,12 @@ impl<T, E> Result<T, E> { | |||
/// ``` | |||
/// use std::io::{BufReader, IoResult}; | |||
/// | |||
/// let buffer = "1\n2\n3\n4\n"; | |||
/// let mut reader = BufReader::new(buffer.as_bytes()); | |||
/// let mut buffer = "1\n2\n3\n4\n"; |
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 needs a .as_bytes()
.
This continues the work @thestinger started in #18885 (which hasn't landed yet, so wait for that to land before landing this one). Instead of adding more methods to `BufReader`, this just allows a `&[u8]` to be used directly as a `Reader`. It also adds an impl of `Writer` for `&mut [u8]`.
rust-lang/rust#18980 This implements Reader for us. With this commit we finished using both MemWriter & MemReader.
minor: Sync from downstream
This continues the work @thestinger started in #18885 (which hasn't landed yet, so wait for that to land before landing this one). Instead of adding more methods to
BufReader
, this just allows a&[u8]
to be used directly as aReader
. It also adds an impl ofWriter
for&mut [u8]
.