std.io.Writer
and
std.io.Reader
provide standard ways of making use of IO. std.ArrayList(u8) has a writer
method which gives us a writer. Let’s use it.
Here we will use a reader to copy the file’s contents into an allocated buffer.
The second argument of
readAllAlloc
is the maximum size that it may allocate; if the file is larger than this, it
will return error.StreamTooLong.
A common usecase for readers is to read until the next line (e.g. for user
input). Here we will do this with the
std.io.getStdIn()
file.
An
std.io.Writer
type consists of a context type, error set, and a write function. The write
function must take in the context type and a byte slice. The write function must
also return an error union of the Writer type’s error set and the number of
bytes written. Let’s create a type that implements a writer.