Io module

writer_of

procedureextensionexperimental3 overloadsSource

The file as a Writer. Writing advances the file's position. The writer points at the Io_File, which must outlive it.

Example
Io :: #import "Extensions/Io";
#import "File";

path := "/tmp/jacket-io-writer-of.txt";
Io.io_run(() {
    file, _ := Io.open_file("/tmp/jacket-io-writer-of.txt", .WRITE);
    defer Io.close(*file);

    count, error := Io.write(Io.writer_of(*file), "hello");
    assert(error == .NONE && count == 5);
});
file_delete(path);

Doesn't run in the browser (only on linux, macos).

writer_of :: (file: *Io_File) -> Writer

Parameters

file: *Io_File

Returns

writer_of :: (stream: Tcp_Stream) -> Writer

The TCP stream as a Writer. write through it waits as long as it takes for the peer to take the bytes. Closing the writer closes the stream.

Parameters

stream: Tcp_Stream

Returns

Example↗
Io :: #import "Extensions/Io";

main :: () {
    Io.io_run(() {
        listener, _ := Io.tcp_listen("127.0.0.1:0");
        defer Io.close(listener);

        address, _ := Io.local_address(listener);
        client, _ := Io.connect(address);
        server, _ := Io.accept(listener);

        Io.write(Io.writer_of(client), "hello");

        buffer: [8] u8;
        count, error := Io.read(Io.reader_of(server), buffer);
        assert(error == .NONE && string.{ count, buffer.data } == "hello");

        Io.close(client);
        Io.close(server);
    });
}
writer_of :: (stream: *Pipe_Stream) -> Writer

One end of a pipe from make_pipe as a Writer. The writer points at the pipe end, which must outlive it.

Parameters

stream: *Pipe_Stream

Returns

Example↗
Io :: #import "Extensions/Io";

Io.io_run(() {
    near, far := Io.make_pipe();
    defer Io.close(near);
    defer Io.close(far);
    Io.write(Io.writer_of(far), "ping");

    buffer: [8] u8;
    count, _ := Io.read(Io.reader_of(near), buffer);
    assert(string.{ count, buffer.data } == "ping");
});

See also