io
lib/io.vklib/io.vk
Pure-Veko ORO-native Reader, Writer, Stream abstractions, in-memory streams, and SchemeMux (mem://).
Zero capabilities required.
ReadStep = struct { chunk bytes, state any }
Result of a single stream read step carrying the read chunk and updated ORO stream state.
WriteStep = struct { written i64, state any }
Result of a single stream write step carrying bytes written and updated ORO stream state.
Stream = struct { name str, closed bool = ..., state any = ..., read_step fn[any,i64->ReadStep], write_step fn[any,bytes->WriteStep], close_step fn[any->any] }
Generic byte stream supporting read, write, and close over ORO value state.
SchemeMux = struct { openers map[fn[str->Stream]] = ... }
URI scheme multiplexer mapping scheme names (such as 'mem') to stream opener closures.
mem-stream = fn [initial bytes] Stream
Creates an in-memory read/write Stream initialized with initial bytes.
use [io]
mut s = io/mem-stream (bytes/from-str 'hello ')
_ = io/write mut s (bytes/from-str 'veko')
all = io/read-all mut s
if (bytes/to-str all) != 'hello veko' [ err 'io mem-stream' ] else [ .ok ]
read = fn [mut s Stream, max_n i64] bytes
Reads up to max_n bytes from s (returns empty bytes on EOF).
write = fn [mut s Stream, data bytes] i64
Writes data to s, returning the number of bytes written.
write-all = fn [mut s Stream, data bytes] i64
Writes every byte of data to s, looping on short writes.
copy = fn [mut src Stream, mut dst Stream, chunk_size i64] i64
Copies all remaining bytes from src into dst in chunk_size slices, returning total bytes copied.
use [io]
mut src = io/mem-stream (bytes/from-str 'stream-copy-ok')
mut dst = io/mem-stream (bytes/with-cap 0)
n = io/copy mut src mut dst 4
out = io/read-all mut dst
if n != 14 or (bytes/to-str out) != 'stream-copy-ok' [ err 'io/copy' ] else [ .ok ]
mux-register = fn [mut mux SchemeMux, scheme str, opener fn[str->Stream]] i64
Registers a scheme handler opener = fn [rest str] Stream [ ... ] under scheme.
mux-default = fn [] SchemeMux
Creates a default SchemeMux pre-registered with mem://<initial-utf8>.
use [io]
mux = io/mux-default
mut s = io/mux-open mux 'mem://hello-scheme'
out = io/read-all mut s
if (bytes/to-str out) != 'hello-scheme' [ err 'mux-open' ] else [ .ok ]
mux-open = fn [mux SchemeMux, uri str] Stream
Opens a Stream for uri (e.g. 'mem://payload') by dispatching on the URI scheme before ://.