Module
LinkProcess streams carry input and output. File resources share a cursor and closed state across aliases. Use std.fs for operations on complete file contents.
Why
LinkStream operations expose consumption and lifetime. Whole file replacement already has its own meaning in std.fs.
Availability
LinkImport std.io in language mode 5.21. The current compiler selects std/io.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Public API
LinkPending contracts
LinkRead standard input through its end
LinkRead standard input through its end
read reads the remaining standard input bytes through EOF. UTF-8 validity and text preservation follow UTF-8 decoding. It does not append or remove a newline.
No remaining bytes produces an empty string. A later call reads from the stream position left by the earlier call and therefore returns an empty string at EOF. A terminal follows the same EOF rule as redirected input.
The call may wait for input or EOF. Reading consumes input even when the result is later discarded. A host read failure or invalid UTF-8 produces a structured runtime fault at the readStdin call because the function returns a string rather than a Result.
The Topaz 6.0 host input fault is TPZ4008. Its primary location is the readStdin call.
Why
Standard input is a stream shared with the host. Consuming it once gives EOF one meaning and lets terminal and redirected input obey the same rule. Strict UTF-8 prevents malformed bytes from silently becoming different text.
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 4 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 174 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 311 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 448 -
topaz.stdlib.contract.stdin-consumptionatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Preserve redirected text and its line endings
Linkimport std.iolet text = io.readStdin()io.writeLine(text)
alpha 한글
Expected result
Expected result
Execution completes. Exit code 0.
alpha한글
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 4 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 174 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 311 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 448 -
topaz.stdlib.contract.stdin-consumptionatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Return empty text at an empty input stream
Linkimport std.iolet text = io.readStdin()let empty = text == ""io.writeLine("{empty}")
true
Expected result
Expected result
Execution completes. Exit code 0.
true
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 4 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 174 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 311 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 448 -
topaz.stdlib.contract.stdin-consumptionatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Observe EOF after standard input has been consumed
Linkimport std.iolet first = io.readStdin()let second = io.readStdin()let atEnd = second == ""io.writeLine(first)io.writeLine("{atEnd}")
alpha true
Expected result
Expected result
Execution completes. Exit code 0.
alphatrue
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 4 - compiler/crates/topaz_self_frontend/topaz/emitter/builtins.tpz at
912a5e86e1f9· 174 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 311 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 448 -
topaz.stdlib.contract.stdin-consumptionatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Use a file handle with a shared cursor
LinkUse a file handle with a shared cursor
open evaluates its path once and opens an existing regular file for reading and writing. It does not create or truncate a file. A successful result is Ok containing a File with the cursor at the first byte.
String paths use the host path syntax. Relative paths resolve against the working directory for an ordinary installed CLI invocation. A package host applies its configured filesystem base and capability policy. NUL, an empty path, a missing file, a directory, and denied read or write access return Err containing an error string.
An open error follows the filesystem error context contract with open as the public operation. An error result does not grant a handle, and path validation does not bypass operating system or package permissions.
File values refer to a resource identity. Copies share the open state and cursor. The handle continues to identify the opened file when the directory entry is renamed or replaced. This is the host resource exception in collection value semantics.
read consumes the remaining bytes from the shared cursor through EOF. UTF-8 validity and text preservation follow UTF-8 decoding. A successful read returns Ok containing text, and a successful read at EOF returns an empty string.
A read error returns Err containing an error string without a text value. Bytes already consumed before a host or decoding error are not restored. Callers must not assume the previous cursor position after a read error.
write encodes its string as UTF-8 and writes all bytes at the shared cursor. It adds no newline. Success advances the cursor by the encoded byte count, overwrites those bytes, and preserves an existing suffix beyond them. A write at EOF extends the file.
An empty write succeeds without moving the cursor. A closed handle, denied access, or host write failure returns Err. A host failure may leave a written prefix and an advanced cursor. It does not roll back earlier argument effects or file changes.
close returns unit, releases the resource, and marks every alias closed. Closing an already closed resource also returns unit. A later read or write through any alias returns Err. A host failure while closing produces a structured runtime fault at the close call.
The Topaz 6.0 host close fault is TPZ4009. Its primary location is the close call. A successful write or close does not promise persistence across a system crash.
Receiver and argument expressions are evaluated once in source order before each resource operation. Use std.fs for whole file replacement or whole file reads. Use File when successive operations must share a cursor and lifetime.
Why
A handle gives successive operations one cursor and one lifetime. Whole file operations already belong to std.fs, so rereading and replacing by path would duplicate their meaning. An existing file open avoids surprising creation or truncation, and recoverable access failures stay in Result.
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Open and close an existing file without changing its bytes
Linkimport std.ioimport std.fsimport std.testlet file = test.assertOk(io.open("source.txt"))file.close()io.writeLine(test.assertOk(fs.readText("source.txt")))let missing = test.assertErr(io.open("missing.txt"))let named = missing.contains("missing.txt")io.writeLine("missing {named}")
original missing true
original
Expected result
Expected result
Execution completes. Exit code 0.
originalmissing true
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Reject a directory when opening a file
Linkimport std.iomatch (io.open(".")) {case Err(_) => io.writeLine("directory rejected")case Ok(file) => {file.close()io.writeLine("directory accepted")}}
directory rejected
Expected result
Expected result
Execution completes. Exit code 0.
directory rejected
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Share the cursor across file aliases
Linkimport std.ioimport std.testlet file = test.assertOk(io.open("source.txt"))let alias = fileio.writeLine(test.assertOk(file.read()))let tail = test.assertOk(alias.read())let atEnd = tail == ""io.writeLine("{atEnd}")alias.close()
alpha true
alpha
Expected result
Expected result
Execution completes. Exit code 0.
alphatrue
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Write at the cursor and retain the rest of the file
Linkimport std.ioimport std.fsimport std.testlet file = test.assertOk(io.open("source.txt"))test.assertOk(file.write("XY"))let tail = test.assertOk(file.read())io.writeLine("tail {tail}")file.close()let whole = test.assertOk(fs.readText("source.txt"))io.writeLine("whole {whole}")
tail cdef whole XYcdef
abcdef
Expected result
Expected result
Execution completes. Exit code 0.
tail cdefwhole XYcdef
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Extend a file after reading through EOF
Linkimport std.ioimport std.fsimport std.testlet file = test.assertOk(io.open("source.txt"))test.assertOk(file.read())test.assertOk(file.write("β"))file.close()io.writeLine(test.assertOk(fs.readText("source.txt")))
alphaβ
alpha
Expected result
Expected result
Execution completes. Exit code 0.
alphaβ
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Close every alias and reject later access
Linkimport std.ioimport std.fsimport std.testlet file = test.assertOk(io.open("source.txt"))let alias = filefile.close()alias.close()test.assertErr(alias.read())io.writeLine("read rejected")test.assertErr(alias.write("changed"))io.writeLine("write rejected")io.writeLine(test.assertOk(fs.readText("source.txt")))
read rejected write rejected original
original
Expected result
Expected result
Execution completes. Exit code 0.
read rejectedwrite rejectedoriginal
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Return a file read error for invalid UTF-8
Linkimport std.ioimport std.fsimport std.bytes { Bytes }import std.testlet invalid = test.assertOk(Bytes.fromArray([255]))test.assertOk(fs.writeBytes("invalid.txt", invalid))let file = test.assertOk(io.open("invalid.txt"))test.assertErr(file.read())file.close()io.writeLine("invalid text rejected")
invalid text rejected
Expected result
Expected result
Execution completes. Exit code 0.
invalid text rejected
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi