Zero-copy I/O core: memfile-backed inputs, reusable outputs.
Replaces streams throughout the library. Input is a read cursor over either a memory-mapped file (zero-copy, no read syscalls, no buffer copies) or an owned/shared string. Output is a reusable growable buffer: pass the same Output across calls and reset it to avoid repeated allocation. Span is a borrowed pointer+length view, valid only while its parent Input/Output lives.
Types
Input = ref object of RootObj
- Read cursor. One of str (string-backed) or map (mmap-backed) keeps the memory alive; keep extends that to derived views (e.g. Output converted via toInput).
IoError = object of CatchableError
MapHandle = ref object size*: int
- Owns a read-only file mapping. Closed idempotently.
Output = ref object of RootObj len*: int
- Reusable write buffer. buf may over-allocate; len is used.
Span = object p*: pointer len*: int
- Borrowed view into Input/Output memory. Copy is a memcpy; the parent must outlive any use.
Procs
proc basePointer(inp: Input): pointer {....raises: [], tags: [], forbids: [].}
proc openInputBytes(data: openArray[byte]): Input {....raises: [], tags: [], forbids: [].}
proc openInputCopy(p: pointer; length: int): Input {....raises: [], tags: [], forbids: [].}
- Input owning a copy of [p, p+length).
proc openInputFile(path: string): Input {....raises: [IOError, OSError], tags: [], forbids: [].}
- Zero-copy input over a memory-mapped file.
proc openInputString(s: string): Input {....raises: [], tags: [], forbids: [].}
- Input sharing s (no copy; string is refcounted).
proc openInputView(s: Span; keep: RootRef): Input {....raises: [], tags: [], forbids: [].}
- Input borrowing a span; keep (usually the parent Input/Output) is retained so the memory outlives this cursor.
proc outputBytes(dst: Output): Span {....raises: [], tags: [], forbids: [].}
- Borrowed view of used bytes.
proc spanToString(s: Span): string {....raises: [], tags: [], forbids: [].}
proc writeBytes(dst: Output; p: pointer; n: int) {....raises: [], tags: [], forbids: [].}