# Easier Reader+Writer chaining

**URL:** <https://ziggit.dev/t/easier-reader-writer-chaining/1379>\
**Category:** Brainstorming\
**Tags:** standard-library\
**Created:** [August 5, 2023, 1:26pm UTC](https://ziggit.dev/t/easier-reader-writer-chaining/1379 "2023-08-05T13:26:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgord9518](https://ziggit.dev/user_avatar/ziggit.dev/mgord9518/32/2342_2.png) [@mgord9518](https://ziggit.dev/u/mgord9518)\
**Post date:** [August 5, 2023, 1:26pm UTC](https://ziggit.dev/t/easier-reader-writer-chaining/1379/1 "2023-08-05T13:26:24Z")

</div>

Coming from Go (where there is `io.Copy`), there appears to be no way in Zig to chain streams together without manually reading into a buffer, then writing the buffer to the Writer in a loop.

Obviously, this would need to happen under the hood regardless, but I am curious as to the reasoning behind not having a function which would lightly abstract it.

Is it just a lack of implementation due to not being sought after enough or has this been discussed before and denied? I can’t find any info on it.

In a recent project of mine, I’ve noticed a block of code that bugs me for one reason or another:

```zig
var bytes_read: usize = 1;

while (bytes_read > 0) {
    bytes_read = try reader.read(buf);

    _ = try writer.write(buf[0..bytes_read]);
}

```

Is this the intended way to read data into a Writer? I’ve looked at the Zig io source code and can’t find any other way. An option I find appealing would be something like this potential method being added to io.Reader:

```zig
try reader.readAllIntoWriter(
    buf,
    writer,
);

```

Or an alternative name, such as io.copy, which would also take the reader as a param:

```zig
try std.io.copy(
    buf,
    writer,
    reader,
);

```

The proposed function would just be a light wrapper over the first example block, reading into and writing from `buf`, until the Reader no longer has data left to read. I’d be happy to send a PR but I figured some discussion would be good first.

---

<div class="post-metadata">

**Author:** ![aiac](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@aiac](https://ziggit.dev/u/aiac)\
**Post date:** [August 5, 2023, 1:34pm UTC](https://ziggit.dev/t/easier-reader-writer-chaining/1379/2 "2023-08-05T13:34:34Z")

</div>

may be what you are looking for

[https://ziglang.org/documentation/master/std/#A;std:fifo.LinearFifo.pump](https://ziglang.org/documentation/master/std/#A;std:fifo.LinearFifo.pump)

---

<div class="post-metadata">

**Author:** ![mgord9518](https://ziggit.dev/user_avatar/ziggit.dev/mgord9518/32/2342_2.png) [@mgord9518](https://ziggit.dev/u/mgord9518)\
**Post date:** [August 5, 2023, 1:42pm UTC](https://ziggit.dev/t/easier-reader-writer-chaining/1379/3 "2023-08-05T13:42:18Z")

</div>

Yeah that’s exactly it! Idk how I managed to miss it

---

<div class="post-metadata">

**Author:** ![aiac](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@aiac](https://ziggit.dev/u/aiac)\
**Post date:** [August 5, 2023, 1:44pm UTC](https://ziggit.dev/t/easier-reader-writer-chaining/1379/4 "2023-08-05T13:44:24Z")

</div>

It may be because it is not in the io module, I also tried to search pump / pipe to find it 😂
