veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

io

lib/io.vk

lib/io.vk Pure-Veko ORO-native Reader, Writer, Stream abstractions, in-memory streams, and SchemeMux (mem://). Zero capabilities required.

ReadStep struct
signature
ReadStep = struct { chunk bytes, state any }

Result of a single stream read step carrying the read chunk and updated ORO stream state.

WriteStep struct
signature
WriteStep = struct { written i64, state any }

Result of a single stream write step carrying bytes written and updated ORO stream state.

Stream struct
signature
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.

MemState struct
signature
MemState = struct { buf bytes, pos i64 = ... }

Internal backing state for io/mem-stream.

SchemeMux struct
signature
SchemeMux = struct { openers map[fn[str->Stream]] = ... }

URI scheme multiplexer mapping scheme names (such as 'mem') to stream opener closures.

signature
mem-stream = fn [initial bytes] Stream

Creates an in-memory read/write Stream initialized with initial bytes.

Executable Example
veko
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
signature
read = fn [mut s Stream, max_n i64] bytes

Reads up to max_n bytes from s (returns empty bytes on EOF).

signature
write = fn [mut s Stream, data bytes] i64

Writes data to s, returning the number of bytes written.

signature
close = fn [mut s Stream] enum

Closes s cleanly and returns .ok.

signature
read-all = fn [mut s Stream] bytes

Reads all remaining bytes from s until EOF.

signature
write-all = fn [mut s Stream, data bytes] i64

Writes every byte of data to s, looping on short writes.

copy fn
signature
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.

Executable Example
veko
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 ]
signature
mux-new = fn [] SchemeMux

Creates an empty SchemeMux.

signature
mux-register = fn [mut mux SchemeMux, scheme str, opener fn[str->Stream]] i64

Registers a scheme handler opener = fn [rest str] Stream [ ... ] under scheme.

signature
mux-default = fn [] SchemeMux

Creates a default SchemeMux pre-registered with mem://<initial-utf8>.

Executable Example
veko
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 ]
signature
mux-open = fn [mux SchemeMux, uri str] Stream

Opens a Stream for uri (e.g. 'mem://payload') by dispatching on the URI scheme before ://.