IO

Module io

import io

Bytes in, bytes out — described as attributes, not classes.

io describes streams rather than implementing them. Reader and Writer are attributes, so any type becomes a stream by carrying one — a file, a socket, or a buffer you wrote yourself. Functions that move bytes take @io.Reader or @io.Writer and never care which.

ReadStream and WriteStream are simply the ones the host hands out; os.stdout() returns a WriteStream, and an open fs.File carries both attributes at once.

read and write are how bytes actually move: io.read(stdin, 1) rather than io.Reader(stdin).read(stdin, 1), the way len stands in for @Countable. They are also the switch points routines give way at (ZE-025), and that holds whatever reader or writer they are handed — so a fake that answers instantly interleaves exactly where the host’s own stream would, and cannot starve the other routines. Calling the attribute directly still works and is plain Zirric code, switching only where its own body does.

The Has… attributes are the other half: a program declares that its environment provides a writer, and a test passes one that collects into memory instead of reaching for standard output.

Contents


Data

ReadStream

`io/reader-writer.zirr:35`
data ReadStream {
	readFrom: fn(Int) -> Binary
}

A readable stream backed by the host, such as standard input or an open file.
Any type can be a reader by carrying @Reader; this is simply the one the host hands out.

Fields

Field Description
readFrom Reads up to the given number of bytes from the host.

WriteStream

`io/reader-writer.zirr:42`
data WriteStream {
	writeTo: fn(Binary) -> Int
}

A writable stream backed by the host, such as standard output or an open file.

Fields

Field Description
writeTo Writes the buffer to the host and returns the number of bytes written.

Attributes

HasErrorWriter

`io/capabilities.zirr:12`
attr HasErrorWriter {
	writer(self) -> @Writer
}

Provides a standard error writer.
In production, this usually points to os.stderr.

Fields

Field Description
writer Returns the standard error writer this value provides.

HasStandardReader

`io/capabilities.zirr:19`
attr HasStandardReader {
	reader(self) -> @Reader
}

Provides a standard input writer.
In production, this usually points to os.stdin.

Fields

Field Description
reader Returns the standard input reader this value provides.

HasStandardWriter

`io/capabilities.zirr:5`
attr HasStandardWriter {
	writer(self) -> @Writer
}

Provides a standard out writer.
In production, this usually points to os.stdout.

Fields

Field Description
writer Returns the standard output writer this value provides.

Reader

`io/reader-writer.zirr:4`
attr Reader {
	read(self: @Reader, length: Int) -> Binary
}

Marks a type as a readable stream of bytes.

Fields

Field Description
read Reads up to the given number of bytes. An empty result means the stream is exhausted.

Writer

`io/reader-writer.zirr:10`
attr Writer {
	write(self: @Writer, buf: Binary) -> Int
}

Marks a type as a writable stream of bytes.

Fields

Field Description
write Writes the given buffer and returns the number of bytes written.

Functions

read

`io/reader-writer.zirr:20`
fn read(reader: @Reader, length: Int) -> Binary

Reads up to length bytes from any reader. An empty result means the stream is exhausted.
This is the way to read: it gives the other routines a turn first, so a reader a test supplies interleaves exactly where the host’s own would.


write

`io/reader-writer.zirr:27`
fn write(writer: @Writer, buf: Binary) -> Int

Writes buf to any writer and returns the number of bytes written.
This is the way to write, for the same reason read is the way to read.