Skip to content

Commit 876f00a

Browse files
authoredJun 18, 2023
Rollup merge of #107200 - mina86:c, r=Amanieu
io: soften ‘at most one write attempt’ requirement in io::Write::write At the moment, documentation of std::io::Write::write indicates that call to it ‘represents at most one attempt to write to any wrapped object’. It seems that such wording was put there to contrast it with pre-1.0 interface which attempted to write all the data (it has since been changed in [RFC 517]). However, the requirement puts unnecessary constraints and may complicate adaptors which perform non-trivial transformations on the data. For example, they may maintain an internal buffer which needs to be written out before the write method accepts more data. It might be natural to code the method such that it flushes the buffer and then grabs another chunk of user data. With the current wording in the documentation, the adaptor would be forced to return Ok(0). This commit softens the wording such that implementations can choose code structure which makes most sense for their particular use case. While at it, elaborate on the meaning of `Ok(0)` return pointing out that the write_all methods interprets it as an error. [RFC 517]: https://rust-lang.github.io/rfcs/0517-io-os-reform.html
2 parents 0c2c243 + 7d57cd5 commit 876f00a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed
 

‎library/std/src/io/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1416,17 +1416,18 @@ pub trait Write {
14161416
///
14171417
/// This function will attempt to write the entire contents of `buf`, but
14181418
/// the entire write might not succeed, or the write may also generate an
1419-
/// error. A call to `write` represents *at most one* attempt to write to
1419+
/// error. Typically, a call to `write` represents one attempt to write to
14201420
/// any wrapped object.
14211421
///
14221422
/// Calls to `write` are not guaranteed to block waiting for data to be
14231423
/// written, and a write which would otherwise block can be indicated through
14241424
/// an [`Err`] variant.
14251425
///
1426-
/// If the return value is [`Ok(n)`] then it must be guaranteed that
1427-
/// `n <= buf.len()`. A return value of `0` typically means that the
1428-
/// underlying object is no longer able to accept bytes and will likely not
1429-
/// be able to in the future as well, or that the buffer provided is empty.
1426+
/// If this method consumed `n > 0` bytes of `buf` it must return [`Ok(n)`].
1427+
/// If the return value is `Ok(n)` then `n` must satisfy `n <= buf.len()`.
1428+
/// Unless `buf` is empty, this function shouldn’t return `Ok(0)` since the
1429+
/// caller may interpret that as an error. To indicate lack of space,
1430+
/// implementors should return [`ErrorKind::StorageFull`] error instead.
14301431
///
14311432
/// # Errors
14321433
///

0 commit comments

Comments
 (0)
Please sign in to comment.