# read\_exact and EOF

**URL:** <https://internals.rust-lang.org/t/read-exact-and-eof/3047>\
**Category:** libs\
**Created:** [January 3, 2016, 11:19am UTC](https://internals.rust-lang.org/t/read-exact-and-eof/3047 "2016-01-03T11:19:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [January 3, 2016, 11:19am UTC](https://internals.rust-lang.org/t/read-exact-and-eof/3047/1 "2016-01-03T11:19:17Z")

</div>

I think the `read_exact` method on `std::io::Read` is missing a fairly important signal: 0 bytes were read into the buffer and an EOF was encountered.

For example, you might be reading a file of fixed-size records, and you want to _either_ read an entire record, or know you’ve reached EOF and no data is left to read ( **note** : you need to read from a stream to know whether you’re at EOF or not), or signal something like “incomplete record at end file”. Currently, `read_exact` will return `UnexpectedEof` in both of these last two cases, even though in the former case an EOF might not at all be unexpected.

Should an additional error code be added to indicate this?

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [January 3, 2016, 11:26am UTC](https://internals.rust-lang.org/t/read-exact-and-eof/3047/2 "2016-01-03T11:26:24Z")

</div>

Another possibility would be to give `UnexpectedEof` a payload that records the number of bytes that _were_ read before it hit EOF.

The _downside_ to this, of course, would be that `io::ErrorKind` will suddenly go from one byte to _two pointers_.

Another other possibility would be to give `read_exact` a custom error type along the lines of:

```rust
enum ReadExactError { Partial(usize), Other(io::Error) }

```

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [January 3, 2016, 11:28am UTC](https://internals.rust-lang.org/t/read-exact-and-eof/3047/3 "2016-01-03T11:28:18Z")

</div>

In the [RFC discussion for `read_exact`](https://github.com/rust-lang/rfcs/pull/980) most people seemed pretty adamant that they didn’t want to increase the size of ErrorKind. I also think we shouldn’t have a custom return type as that breaks chaining with `try!` etc.

I’d be ok with a `Result<usize,io:Error>` which is guaranteed to either return `Ok(0)` on immediate EOF, or `Ok(buflen)` when read exactly and `Err(_)` otherwise.

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [January 3, 2016, 11:36am UTC](https://internals.rust-lang.org/t/read-exact-and-eof/3047/4 "2016-01-03T11:36:32Z")

</div>

Just to be clear, a custom error type _would not_ break `try!`, provided you have `impl From<ReadExactError> for io::Error` somewhere. You could also make using the combinators _relatively_ painless by having a shorthand `into_io_error` method. Both of these could just turn `Partial` into `UnexpectedEof`.

I'd personally be leery of having `read_exact` return "success", despite not actually reading anything. `read_exact` should be a convenience method; if you have to wrap it in _yet another_ wrapper to get the "obvious" behaviour, it's not doing its job.

Also, for those wondering: [RFC #980](https://github.com/rust-lang/rfcs/blob/master/text/0980-read-exact.md), and it contains the following:

> In the same way, if this method fails, there is no way to determine how many bytes were read before it determined it couldn't completely fill the buffer.
> 
> Situations that require lower level control can still use `read` directly.

---

<div class="post-metadata">

**Author:** ![jethrogb](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@jethrogb](https://internals.rust-lang.org/u/jethrogb)\
**Post date:** [January 3, 2016, 11:49am UTC](https://internals.rust-lang.org/t/read-exact-and-eof/3047/5 "2016-01-03T11:49:09Z")

</div>

> [@DanielKeep](#):
>
> I'd personally be leery of having read\_exact return "success", despite not actually reading anything. read\_exact should be a convenience method; if you have to wrap it in yet another wrapper to get the "obvious" behaviour, it's not doing its job.

Alternatively: `Result<bool,io::Error>`, this would allow one to write:

```rust
while (try!(stream.read_exact(&mut buf)) {
    ...
}

```

---

<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:25am UTC](https://internals.rust-lang.org/t/read-exact-and-eof/3047/6 "2019-03-25T08:25:38Z")

</div>

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

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