Skip to content

Commit

Permalink
Add WriteAllError trait bound to write_all
Browse files Browse the repository at this point in the history
This is needed to construct an instance on an `Ok(0)` return value as well as
testing for whether the operation was interrupted or not.
  • Loading branch information
alexcrichton committed Jan 23, 2015
1 parent c22c6c6 commit e439eb5
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions text/0517-io-os-reform.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,18 @@ trait Write {
}

trait WriteExt {
fn write_all(&mut self, buf: &[u8]) -> Result<(), Err> { ... };
fn write_fmt(&mut self, fmt: &fmt::Arguments) -> Result<(), Err> { ... }
fn write_all(&mut self, buf: &[u8]) -> Result<(), Err>
where Self::Err: WriteAllError { ... };
fn write_fmt(&mut self, fmt: &fmt::Arguments) -> Result<(), Err>
where Self::Err: WriteAllError { ... };
}

impl<W: Write> WriteExt for W {}

trait WriteAllError: Sized {
fn eof() -> Self;
fn interrupted(&self) -> bool;
}
```

The biggest change here is to the semantics of `write`. Instead of
Expand All @@ -604,6 +612,11 @@ complete, the intermediate result (of how much has been written) is
discarded. To meaningfully recover from an intermediate error, code
should work with `write` directly.

A trait bound of `WriteAllError` is also imposed on the error type for the
`write_all` and `write_fmt` methods to construct an "end of file" related error
when `Ok(0)` is returned from `write` as well as detecting intermittent errors
like `EINTR` that can be "safely ignored" to try to continue writing data.

The `write_fmt` method, like `write_all`, will loop until its entire
input is written or an error occurs.

Expand Down Expand Up @@ -741,9 +754,10 @@ impl Write for Sink { type Err = Void; ... }

// Copies all data from a `Read` to a `Write`, returning the amount of data
// copied.
pub fn copy<E, R, W>(r: &mut R, w: &mut W) -> Result<u64, E> where
R: Read<Err = E>,
W: Write<Err = E>
pub fn copy<E, R, W>(r: &mut R, w: &mut W) -> Result<u64, E>
where R: Read<Err = E>,
W: Write<Err = E>,
R::Err: WriteAllError
```

Like `write_all`, the `copy` method will discard the amount of data already
Expand Down

0 comments on commit e439eb5

Please sign in to comment.