Io module

Reader

structSource

A source of bytes: a pointer to its data and the procedures to use on it. reader_of makes one for a file, a TCP stream or a pipe end, and a struct of your own can fill in read_proc (and optionally close_proc and wait_proc). A Reader is a handle: copy it freely, it owns nothing, and the source must stay alive while it is used. close on it closes the source.

Example
Counter :: struct {
    next: u8;
}

count_up :: (data: *void, buffer: [] u8) -> s64, Io_Error {
    counter := cast(*Counter) data;

    for 0..buffer.count - 1 {
        buffer[it] = counter.next;
        counter.next += 1;
    }

    return buffer.count, .NONE;
}

main :: () {
    counter: Counter;
    source := Reader.{ data = *counter, read_proc = count_up };

    bytes: [4] u8;
    count, error := read(source, bytes);

    assert(error == .NONE && count == 4);
    assert(bytes[0] == 0 && bytes[3] == 3);
}

Doesn't run in the browser (skipped on wasm).

Fields

data: *void
read_proc: Read_Proc
close_proc: Close_Proc
Null for a source that has nothing to close.
wait_proc: Wait_Proc
Null for a source that cannot say when a read would not wait (wait_readable then returns .UNSUPPORTED). A TCP stream can.

See also