std.Io.Writer that writes in-memory (probably to an std.ArrayList or similar)?

Hi there,

I want to use std.http.Client to make an https request and capture the body of the response. I’ve tried this and it works perfectly, but the writer I passed to std.http.Client.fetch was &std.fs.File.stdout().writer(…).interface, so the output went to stdout. This was perfect for testing, but I want to manipulate the data and show the user something based on it. I tried looking through the stdlib docs but I couldn’t seem to find an implementation of an Io.Writer that simply outputs to a chunk of heap-allocated memory (e.g. an std.ArrayList or similar). I’m just about to go to sleep, it’s far too late here already, so figured I’d throw this at all the smart people here and see what I’m overlooking.

Thanks in advance!

std.Io.Writer.Allocating, it can be converted to/from an ArrayList, though you likely don’t need that.

There is also std.Io.Writer.fixed if you don’t want it to grow during writing. It also works with stack buffers.

7 Likes

Derp. I knew it had to be stupid simple. No idea why I never thought to click on that type, whenever I heard the name I just kept scrolling past it. Thanks a ton!

1 Like