Files
ww/lib/io/io.ww
Hojun-Cho 09a336cb74 lib+test/wcc: add memio, rename io stream.ww→io.ww
Mirrors Hare's memio: fixed, dynamic, dynamicfrom, buffer, string,
reset, borrowedread. Caller owns the state + stream slots because
w6c lacks &x.field and 32B return-by-value. Tests table-driven via
parallel arrays. io.stream now exported.
2026-05-13 16:07:18 +09:00

34 lines
1.0 KiB
Plaintext

// io — stream interface (Plan 9 Bio / Hare io::stream shape).
//
// No closures, no methods. A `stream` is a struct of function
// pointers plus a `ctx: *void`. The error channel is the return
// value of read/write/close — Hare-shaped tagged unions instead of
// errno-style integer sentinels.
// eof — read past the end of the stream. NAMED-void so it's a
// distinct variant tag from `void` (which would be "no result yet").
export type eof = void;
// closed — operation attempted on a stream that has already been
// closed. NAMED-void; same shape, different tag.
export type closed = void;
export type stream = struct {
ctx: *void,
read: fn(s: *stream, buf: []u8) (i32 | eof | closed),
write: fn(s: *stream, buf: []u8) (i32 | closed),
close: fn(s: *stream) (void | closed),
};
export fn read(s: *stream, buf: []u8) (i32 | eof | closed) = {
return s.read(s, buf);
};
export fn write(s: *stream, buf: []u8) (i32 | closed) = {
return s.write(s, buf);
};
export fn close(s: *stream) (void | closed) = {
return s.close(s);
};