# std::io::Write: "\[a\] call to write represents at most one attempt to write to any wrapped object"

**URL:** <https://internals.rust-lang.org/t/std-io-write-a-call-to-write-represents-at-most-one-attempt-to-write-to-any-wrapped-object/5068>\
**Category:** libs\
**Created:** [April 10, 2017, 11:55pm UTC](https://internals.rust-lang.org/t/std-io-write-a-call-to-write-represents-at-most-one-attempt-to-write-to-any-wrapped-object/5068 "2017-04-10T23:55:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunshowers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sunshowers/32/7267_2.png) [@sunshowers](https://internals.rust-lang.org/u/sunshowers)\
**Post date:** [April 10, 2017, 11:55pm UTC](https://internals.rust-lang.org/t/std-io-write-a-call-to-write-represents-at-most-one-attempt-to-write-to-any-wrapped-object/5068/1 "2017-04-10T23:55:19Z")

</div>

[The documentation for Write::write](https://doc.rust-lang.org/std/io/trait.Write.html#tymethod.write) says that

> A call to write represents at most one attempt to write to any wrapped object.

A strict interpretation of this poses problems for writer wrappers like compressors that have to buffer data, process it, and then write it out. If the internal buffer is full, the only thing `write` can do is write the internal buffer out and then return an error indicating that the caller must retry (the only reasonable option here is `ErrorKind::Interrupted`).

However, [in this GitHub comment](https://github.com/tokio-rs/tokio-io/issues/37#issuecomment-293100734) @alexcrichton says that

> Note that the comment in the docs there was largely targeted at atomicity. Basically if you successfully write any bytes then that must be reported back. I think it's fine, semantically, for an auto-retry to happen on EINTR.

This viewpoint makes sense, but it doesn't really match my interpretation of the documentation. I also don't know what the pitfalls of this would be.

Is it OK for a wrapped writer to call the inner `write` multiple times at all? If it is, the documentation should probably be updated to reflect that.

---

<div class="post-metadata">

**Author:** ![ehiggs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ehiggs/32/3224_2.png) [@ehiggs](https://internals.rust-lang.org/u/ehiggs)\
**Post date:** [April 11, 2017, 3:17pm UTC](https://internals.rust-lang.org/t/std-io-write-a-call-to-write-represents-at-most-one-attempt-to-write-to-any-wrapped-object/5068/2 "2017-04-11T15:17:30Z")

</div>

It depends on how high or low level you view your writer. If you’re at the low level of the stdlib, you should expose the underlying errors. If you’re giving some convenience function, it’s almost certainly fine to eat annoying errors like EINTR which just allow for bugs.

I mean, do you expect people to call your code using

```rust
mystruct.write(&buf)?;

```

If so, then this won’t get the chance to retry if you expose EINTR. That’s almost certainly wrong.

EDIT: in fact, I think the docs could definitely be updated to point out that using `std::fs::File::write` and friends using `try!` and `?` is a bad idea for exactly the aforementioned reason.

---

<div class="post-metadata">

**Author:** ![sunshowers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sunshowers/32/7267_2.png) [@sunshowers](https://internals.rust-lang.org/u/sunshowers)\
**Post date:** [April 11, 2017, 3:42pm UTC](https://internals.rust-lang.org/t/std-io-write-a-call-to-write-represents-at-most-one-attempt-to-write-to-any-wrapped-object/5068/3 "2017-04-11T15:42:08Z")

</div>

Hmm, but should typical application code be calling `write` directly at all? In most cases it’s just things like wrappers and combinators that would be calling the lower-level `write`.

In any case the docs seem to be pretty unclear about this.

---

<div class="post-metadata">

**Author:** ![ehiggs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ehiggs/32/3224_2.png) [@ehiggs](https://internals.rust-lang.org/u/ehiggs)\
**Post date:** [April 11, 2017, 4:00pm UTC](https://internals.rust-lang.org/t/std-io-write-a-call-to-write-represents-at-most-one-attempt-to-write-to-any-wrapped-object/5068/4 "2017-04-11T16:00:13Z")

</div>

FWIW, I submitted a documentation issue [here](https://github.com/rust-lang/rust/issues/41219).

> but should typical application code be calling write directly at all?

I should hope so! I thought `Write` was like the [Writer interface in Go](https://golang.org/pkg/io/#Writer): a useful lingua franca concept for something that moves data in one direction.

Otherwise, you can set `SA_RESTART` as a sigaction and let the underlying libc handle restarts to syscalls.

---

<div class="post-metadata">

**Author:** ![gyscos](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gyscos/32/1732_2.png) [@gyscos](https://internals.rust-lang.org/u/gyscos)\
**Post date:** [April 11, 2017, 4:11pm UTC](https://internals.rust-lang.org/t/std-io-write-a-call-to-write-represents-at-most-one-attempt-to-write-to-any-wrapped-object/5068/5 "2017-04-11T16:11:24Z")

</div>

But in this case, `Write` exposes `write_all`, which probably does what people want. `write` itself looks like a lower-level block. Apart from wrappers (and some asynchronous io specifics), I’m not sure when it’d make sense to use `write` instead of `write_all`.

---

<div class="post-metadata">

**Author:** ![alexcrichton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/alexcrichton/32/4501_2.png) [@alexcrichton](https://internals.rust-lang.org/u/alexcrichton)\
**Post date:** [April 11, 2017, 4:34pm UTC](https://internals.rust-lang.org/t/std-io-write-a-call-to-write-represents-at-most-one-attempt-to-write-to-any-wrapped-object/5068/6 "2017-04-11T16:34:36Z")

</div>

This documentation [dates all the way back](https://github.com/rust-lang/rust/pull/21835) to the inception of the I/O module, and I think it’s just a bug in the docs. The intention here was to convey concepts such as:

- `File::write` invokes the `write` syscall at most once.
- `BufWriter::write` will only try to write its internal buffer once
- Implementations of `Write` shouldn’t spin indefinitely if they get back “would block” by accident

etc. Basically all implementations of `Write::write` are intended to be “as low level as they can be” for that particular implementation, sorta mostly defined by that implementation. If your implementation is “retry on EINTR” then I think that’s a perfectly valid `Write` implementation

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:28am UTC](https://internals.rust-lang.org/t/std-io-write-a-call-to-write-represents-at-most-one-attempt-to-write-to-any-wrapped-object/5068/7 "2019-03-25T08:28:20Z")

</div>

Closed on March 25, 2019, 8:28am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
