cli: add explicit owning buffered file adapters
Add pinned std.io.reader and std.io.writer using safe source, explicit fixed allocation and file transfer. Finish/discard return complete resources; writer failures preserve pending data and prohibit unsound partial retries. Specify cursor, acceptance, failure and cleanup contracts in spec 16, RFC 0034, the API reference and source bundle. Add standalone conformance, independent byte oracles, task/GC stress, CLI/package/profile checks and hosted partial-write tests with a raised child quota to detect retries. Validation: 1061 conformance cases; 34 buffered cases with per-step GC and 12 schedule seeds; unit/Quick Reference, formatting, hosted smoke, 89 CLI checks, 115 source coverage constructs and reference generation.
This commit is contained in:
1 parent
3e1c0f34e8
commit
8d8bef8f3e
51 files changed
+1916
-8
No files matched your search
@@ -40,6 +40,8 @@ jobs:
|
||||
- run: python3 tools/heap_vectors.py
|
||||
- run: python3 tests/hosted/source_io.py
|
||||
- run: python3 tools/io_vectors.py
|
||||
- run: python3 tests/hosted/source_buffered_io.py
|
||||
- run: python3 tools/buffered_io_vectors.py
|
||||
- run: zig-out/bin/aji run --grant out=io.write --grant heap=mem.alloc:64 docs/agent/syntax-example.air
|
||||
- run: zig build agent-reference
|
||||
- name: Source construct coverage (Phase 6A)
|
||||
|
||||
@@ -34,6 +34,8 @@ python3 tests/hosted/source_heap.py # explicit ordering, owning extraction and p
|
||||
python3 tools/heap_vectors.py # independent heapq traces and exhaustive small permutations
|
||||
python3 tests/hosted/source_io.py # bounded I/O, profiles, partial errors and explicit resources
|
||||
python3 tools/io_vectors.py # independent byte/count/cursor oracle for read/copy boundaries
|
||||
python3 tests/hosted/source_buffered_io.py # owning adapters, partial failures and no retry
|
||||
python3 tools/buffered_io_vectors.py # independent read-prefix and segmented-write bytes
|
||||
(cd tools/eval && python3 -m unittest discover tests) # evaluation harness self-tests
|
||||
zig fmt --check build.zig src tools
|
||||
```
|
||||
|
||||
@@ -37,6 +37,8 @@ pub fn build(b: *std.Build) void {
|
||||
mod.addAnonymousImport("std_hashset_aji", .{ .root_source_file = b.path("std/hashset.aji") });
|
||||
mod.addAnonymousImport("std_heap_aji", .{ .root_source_file = b.path("std/heap.aji") });
|
||||
mod.addAnonymousImport("std_io_aji", .{ .root_source_file = b.path("std/io.aji") });
|
||||
mod.addAnonymousImport("std_io_reader_aji", .{ .root_source_file = b.path("std/io/reader.aji") });
|
||||
mod.addAnonymousImport("std_io_writer_aji", .{ .root_source_file = b.path("std/io/writer.aji") });
|
||||
mod.addAnonymousImport("std_map_aji", .{ .root_source_file = b.path("std/map.aji") });
|
||||
mod.addAnonymousImport("std_math_aji", .{ .root_source_file = b.path("std/math.aji") });
|
||||
mod.addAnonymousImport("std_option_aji", .{ .root_source_file = b.path("std/option.aji") });
|
||||
|
||||
@@ -127,3 +127,5 @@ RFC 0031 general owning HashSet (cl100k, relative to HashMap): Quick Reference 8
|
||||
RFC 0032 owning BinaryHeap (cl100k, relative to HashSet): Quick Reference 8,201 → 8,284 (+1.0%), other source docs 54,798 → 56,770 (+3.6%), full source excluding AIR 62,999 → 65,054 (+3.3%). library/std.md is 19,911 tokens; AIR remains 179,282 bytes / 51 sections. Eight safe source operations bind explicit pure order and sift Copy indices while complete owners remain in initialized storage. No new syntax, diagnostic or verifier/runtime state; cumulative source growth is +91.4%, justified by visible ordering/ownership/failure/accounting contracts without an empirical MCC claim.
|
||||
|
||||
RFC 0033 bounded I/O (cl100k, relative to BinaryHeap): Quick Reference 8,284 → 8,350 (+0.8%), other source docs 56,770 → 58,473 (+3.0%), full source excluding AIR 65,054 → 66,823 (+2.7%). library/std.md is 21,614 tokens; AIR remains 179,282 bytes / 51 sections. Six safe concrete compositions retain exact effects and caller resource loans, distinguishing EOF/limits from partial failure. No new syntax, diagnostic or verifier/runtime state; cumulative source growth is +96.6%, justified by progress/authority/cleanup contracts without an empirical MCC claim.
|
||||
|
||||
RFC 0034 owning buffered files (cl100k, relative to bounded I/O): Quick Reference 8,350 → 8,445 (+1.1%), other source docs 58,473 → 61,223 (+4.7%), full source excluding AIR 66,823 → 69,668 (+4.3%). library/std.md is 24,364 tokens; AIR remains 179,282 bytes / 51 sections. Ten safe concrete operations keep fixed allocation, file transfer, prefetch and first-error/partial-write cleanup visible. No new syntax, diagnostic or verifier/runtime state; cumulative source growth is +105.0%, justified by these contracts without an empirical MCC claim. Full representation/API details stay on demand; front-door consolidation remains separate work.
|
||||
@@ -371,6 +371,8 @@ The usual enum rules apply: missing arm `E7004`, wrong enum/payload `E7005`, unk
|
||||
|
||||
`std.io` supplies exact stdin/file/stream reads, bounded file read-to-end and limited file copy/append with caller slices and explicit effects/authority. EOF differs from full/limit; errors report confirmed progress. Resources stay borrowed, no heap or close is hidden, and failed external writes may be partial. Full contracts are in `/docs/library/std.md`.
|
||||
|
||||
`std.io.reader`/`std.io.writer` own a file and an explicitly allocated fixed byte buffer. Use new, read/read_exact or write/flush, then finish to return the file for explicit close. Unread/failed finish returns the whole adapter; discard explicitly abandons buffered bytes and frees storage. A failed writer preserves its first error and never retries. Capacity zero uses direct I/O. Effects and ownership remain explicit; see `/docs/library/std.md`.
|
||||
|
||||
`std.hash.fnv1a64` and `crc32c` hash byte slices without allocation; their `_extend` forms continue from a previous result. Hashes do not establish key equality or authentication. Exact algorithms and chunk/byte-order conventions are in `/docs/library/std.md`.
|
||||
|
||||
## 11. Package modules
|
||||
|
||||
@@ -141,4 +141,4 @@ Lowering layers (P6A-3/4), each with accepted (golden AIR), rejected (`E7xxx`) a
|
||||
| P6-9 | todo | Port reference programs and examples to `.aji` | P6-5 | `tests/programs/`, `examples/` | exit criterion passes |
|
||||
| P6-10 | todo | Phase exit review | all | plan status | exit criterion passes |
|
||||
|
||||
The [standard-library track](standard-library.md) implements sixteen compiler-pinned modules and qualified standard enum names. STD-3/RFC 0021 completes P6-3 on the branch with explicit manifest source mapping, acyclic linking, direct qualified names, imported struct literals and task workers. Dependency fetching/resolution is separate package work; STD-4/RFC 0022 completes explicit generics and structural constraints on the branch. STD-5/RFC 0023 adds whole-value linear structs/enums/fixed arrays, complete unpack/match, explicit exchange and ownership-preserving allocation failure through all layers (spec 15 §16). STD-6a/b/RFC 0024 resolves Q-036 with initialized buffer<T> and safe std.vec, explicit growth, returned failure ownership and draining (spec 15 §17). STD-6c/RFC 0025 adds safe owning Deque with existing source primitives. STD-6d.1/RFC 0026 adds pure std.hash; STD-6d.2/RFC 0028 adds safe owning U64Map<V> after the explicit error_values prerequisite (RFC 0027). STD-6d.3/RFC 0029 adds explicit static function parameters for general hash/equality/order operations with existing AIR calls/workers. STD-6d.4/RFC 0030 implements the general owning-key std.hashmap contract; its full GC/stress validation passes and Q-037 is resolved. STD-6d.5/RFC 0031 implements general owning std.hashset with duplicate input return; its conformance, GC/schedule, CLI and documentation checks pass. STD-6d.6/RFC 0032 implements safe owning std.heap with explicit order; conformance, GC/schedule, CLI and documentation checks pass. STD-7a/RFC 0033 implements bounded std.io composition with explicit progress, resource loans and exact effects; conformance, GC/schedule, CLI and documentation checks pass. Buffered adapters and lexical paths remain STD-7b/c. Existing `from`-returns remain unimplemented (Q-034). These increments do not complete Phase 6.
|
||||
The [standard-library track](standard-library.md) implements eighteen compiler-pinned modules and qualified standard enum names. STD-3/RFC 0021 completes P6-3 on the branch with explicit manifest source mapping, acyclic linking, direct qualified names, imported struct literals and task workers. Dependency fetching/resolution is separate package work; STD-4/RFC 0022 completes explicit generics and structural constraints on the branch. STD-5/RFC 0023 adds whole-value linear structs/enums/fixed arrays, complete unpack/match, explicit exchange and ownership-preserving allocation failure through all layers (spec 15 §16). STD-6a/b/RFC 0024 resolves Q-036 with initialized buffer<T> and safe std.vec, explicit growth, returned failure ownership and draining (spec 15 §17). STD-6c/RFC 0025 adds safe owning Deque with existing source primitives. STD-6d.1/RFC 0026 adds pure std.hash; STD-6d.2/RFC 0028 adds safe owning U64Map<V> after the explicit error_values prerequisite (RFC 0027). STD-6d.3/RFC 0029 adds explicit static function parameters for general hash/equality/order operations with existing AIR calls/workers. STD-6d.4/RFC 0030 implements the general owning-key std.hashmap contract; its full GC/stress validation passes and Q-037 is resolved. STD-6d.5/RFC 0031 implements general owning std.hashset with duplicate input return; its conformance, GC/schedule, CLI and documentation checks pass. STD-6d.6/RFC 0032 implements safe owning std.heap with explicit order; conformance, GC/schedule, CLI and documentation checks pass. STD-7a/RFC 0033 implements bounded std.io composition with explicit progress, resource loans and exact effects; conformance, GC/schedule, CLI and documentation checks pass. STD-7b/RFC 0034 implements safe owning buffered file adapters with explicit finish/discard and first-error/no-retry behavior; conformance, GC/schedule, CLI and documentation checks pass. Lexical paths remain STD-7c. Existing `from`-returns remain unimplemented (Q-034). These increments do not complete Phase 6.
|
||||
@@ -20,7 +20,7 @@ Without external source dependencies, Aji should support ordinary CLI tools, sto
|
||||
|---|---|---|
|
||||
| Scalars, structs/enums, slices/arrays, explicit borrows | implemented | pure fixed-storage algorithms can start now |
|
||||
| Heap/regions, I/O, files, TCP, clock/random, structured tasks/channels/mutex | implemented primitives | wrappers can reuse them, but their presence does not imply the complete APIs below |
|
||||
| Built-in standard imports and enum types | STD-0/1a/2a/2b branch implementation | compiler-pinned, closed registry; sixteen modules; qualified enum types/constructors/patterns; no package lookup |
|
||||
| Built-in standard imports and enum types | STD-0/1a/2a/2b branch implementation | compiler-pinned, closed registry; eighteen modules; qualified enum types/constructors/patterns; no package lookup |
|
||||
| General modules/imported struct literals | STD-3 branch implementation (P6-3; RFC Proposed) | explicit manifest mapping, direct qualified names, ordinary verifier |
|
||||
| User generics/structural constraints | STD-4 branch implementation (P6-4; RFC Proposed) | needed for reusable `Vec<T>`, `HashMap<K,V>`, option/result |
|
||||
| `linear_aggregates` | STD-5 branch implementation (RFC Proposed) | whole-value owning structs/enums/fixed arrays; initialized buffer<T>/std.vec implemented by STD-6a/b |
|
||||
@@ -67,7 +67,7 @@ SQL/database clients, Redis/Kafka, ORMs, application authentication, gRPC/protob
|
||||
| STD-6d.6 | done (branch implementation; RFC 0032 Proposed) | owning BinaryHeap<T,B> | STD-6a/6d.3 | explicit pure order, complete resource extraction, bounded push, failure-preserving growth |
|
||||
| STD-7 | in progress | concrete I/O adapters, buffered I/O and lexical paths | STD-1b/2a/2b; STD-3 for public types | short reads, EOF, capacity and write failures covered; paths never grant filesystem authority |
|
||||
| STD-7a | done (branch implementation; RFC 0033 Proposed) | exact/bounded reads and limited file copies | STD-1b/3 | explicit progress/EOF/error, borrowed resources, caller slices and exact effects |
|
||||
| STD-7b | todo | owning buffered concrete file reader/writer | STD-6a/7a | failure ownership, refill/flush, explicit finish/discard and no hidden cleanup |
|
||||
| STD-7b | done (branch implementation; RFC 0034 Proposed) | owning buffered concrete file reader/writer | STD-6a/7a | failure ownership, refill/flush, explicit finish/discard and no hidden cleanup |
|
||||
| STD-7c | todo | caller-buffer lexical paths | STD-1b/2a | pure normalization/join/components with no filesystem authority |
|
||||
| STD-8 | todo | filesystem gaps, args/env capability RFC, time/random distinctions and structured log | STD-3/7; vertical runtime feature where needed | CLI/storage milestone examples work in deterministic and hosted profiles; all authority is granted at entry |
|
||||
| STD-9 | todo | independent integration application and API review | STD-6/7/8 | integration gate; public application is not private benchmark material |
|
||||
@@ -172,3 +172,10 @@ The long general-map GC/stress process also completed with 33/33; Q-037 and the
|
||||
STD-7a/RFC 0033 implements six safe std.io compositions with concrete io.read/fs.read/net.read/fs.write effects, borrowed resources and caller-supplied slices. Exact reads distinguish completed data, EOF and read errors. Bounded read-to-end/copy stop at capacity/limit without consuming an extra byte to probe EOF. Copy errors distinguish confirmed reads from confirmed writes; external partial writes have no rollback promise. No heap, retained borrow, open/close, task/timeout, new syntax/AIR semantics, verifier state or diagnostic is added.
|
||||
|
||||
All 1027 conformance cases pass, including 20 I/O cases. All runtime I/O fixtures pass collection before every instruction; fragmented TCP and restored task loans run 12 schedule seeds under quanta 1, 2, 7 and their own quantum. An independent Python prefix oracle covers 162 exact/bounded reads and 250 copy/append combinations, validating bytes, guards, counts, source cursor tails, limits and empty scratch. Hosted tests produce a real read error and partial external file write: four bytes consumed, two confirmed written, three actually present. Unit/Quick Reference, formatting, hosted smoke, 55 CLI/source/AIR/package/import-order/profile/provenance checks, 111/111 source coverage constructs and bundle generation pass. RFC 0033 records documentation growth. Owning buffered adapters STD-7b and lexical paths STD-7c remain required; STD-7 and Std 0.1 are unfinished.
|
||||
|
||||
|
||||
## Owning buffered file verification
|
||||
|
||||
STD-7b/RFC 0034 implements safe std.io.reader/std.io.writer, each owning a file and a fixed-capacity initialized byte buffer. Only new allocates with explicit authority and failure returns the original file. Reads distinguish cached data/refill/EOF and finish returns unread readers. Writer errors preserve pending bytes and the first error, forbid further writes/retries, and return the whole failed writer from finish. Explicit discard reports abandoned cached/pending bytes, frees storage and returns the file. No file open/close/sync, hidden growth/cleanup or new syntax/AIR/runtime/verifier state is added.
|
||||
|
||||
All 1061 conformance cases pass, including 34 buffered adapter fixtures. Every buffered fixture passes collection before every instruction; worker transfer and restored loans run 12 seeds under quanta 1, 2, 7 and their own quantum. Independent byte oracles cover 48 read streams, 64 exact prefixes and 96 segmented writes. On Linux, 89 CLI/source/AIR/package/profile/provenance checks include a real read fault and four external partial-write cases; raising the child's file quota after failure proves the writer still performs no retry and preserves pending bytes/error. Unit/Quick Reference, formatting, hosted smoke, 115/115 source coverage constructs and reference generation pass. RFC 0034 records documentation growth. Lexical paths STD-7c, system gaps STD-8 and the Std 0.1 exit examples remain required; the full std roadmap is unfinished.
|
||||
+32
-3
@@ -1,8 +1,8 @@
|
||||
# Standard library API
|
||||
|
||||
Normative contracts: spec 16. Import rules: spec 15 §13. These modules are bundled with the compiler, with no external source dependencies. Allocation authority is explicit for std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve.
|
||||
Normative contracts: spec 16. Import rules: spec 15 §13. These modules are bundled with the compiler, with no external source dependencies. Allocation authority is explicit for std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve and std.io.reader/std.io.writer new.
|
||||
|
||||
Except std.io (§2.17) and std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve, functions below declare `effects []`, allocate no heap memory and retain no borrows. Pass arrays/slices using explicit `ref` or `mut`. Names are fully qualified; no alias or short-name import exists.
|
||||
Except std.io/std.io.reader/std.io.writer (§2.17–§2.18) and std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve, functions below declare `effects []`, allocate no heap memory and retain no borrows. Pass arrays/slices using explicit `ref` or `mut`. Names are fully qualified; no alias or short-name import exists.
|
||||
|
||||
| Module | Function signature | Result/failure |
|
||||
|---|---|---|
|
||||
@@ -389,10 +389,39 @@ Import `std.io`. Six concrete functions compose existing primitives with caller-
|
||||
|
||||
ReadExactResult is ok / eof(completed:usize) / err(ReadError{completed:usize,error:io_error}). Exact reads fill the destination, looping over short reads. Empty destination is ok without I/O. EOF leaves the remaining suffix unchanged. A read error guarantees only the successful prefix; the attempted suffix follows the primitive's failure contract. Bytes outside the supplied destination remain unchanged.
|
||||
|
||||
ReadToEndResult is eof(completed:usize) / full(completed:usize) / err(ReadError). Full means the supplied buffer was filled; it does not establish whether EOF was reached or unread data remains. Empty destination is full(0), and no extra byte is read/discarded to probe EOF. Growing/owning read-to-end and buffered adapters are separate work.
|
||||
ReadToEndResult is eof(completed:usize) / full(completed:usize) / err(ReadError). Full means the supplied buffer was filled; it does not establish whether EOF was reached or unread data remains. Empty destination is full(0), and no extra byte is read/discarded to probe EOF. Growing/owning read-to-end is separate work; fixed-capacity adapters are below.
|
||||
|
||||
CopyResult is eof(completed:usize) / limit(completed:usize) / err(CopyError{read:usize,confirmed_written:usize,error:io_error}). Copies bound every scratch chunk by remaining limit and stop without probing past it. Exact EOF at the limit reports limit. A read error has both counts equal to earlier completed chunks. A write error includes its consumed input chunk in read; confirmed_written includes only prior successful writes. The failed write may have externally written a prefix: there is no rollback promise or exact failed-output count. Resources remain borrowed for explicit caller cleanup.
|
||||
|
||||
Zero limit returns limit(0) without I/O, even with empty scratch. Empty scratch with positive limit returns invalid_input with both error counts zero without I/O. No counter exceeds the limit or overflows. Scratch contents after a call are unspecified; bytes outside its slice remain unchanged. The [copy example](../../examples/std/io.aji) uses only fs.read/fs.write entry grants and a fixed frame scratch array, handling results and closing both resources explicitly. Existing fs.write_all/net.send_all/console writes already write all bytes on success, so this increment adds no duplicate write alias.
|
||||
|
||||
Ordinary alias/task-loan, type/mode, capability and effect checks apply; imports grant nothing. Blocking/fuel/profile errors remain spec 11 behavior, with no hidden timeout, task or unsafe boundary. std.io's concrete contracts are spec 16 §2.17; owning buffered adapters and lexical paths remain forthcoming.
|
||||
|
||||
## Owning buffered files
|
||||
|
||||
Import `std.io.reader` for Reader/file<read> and `std.io.writer` for Writer/file<write>. Import `std.io` directly when spelling read_exact's result, and `std.option` when matching the writer failure state; dependency imports do not expose names to callers. Each adapter owns a fixed initialized buffer and its file. Only new allocates, with an explicit mem.alloc capability, charging N bytes. Zero capacity delegates directly to primitive I/O. Files are never implicitly closed or synced. All file/adapter-bearing results are linear: consume every normal branch explicitly.
|
||||
|
||||
| Function arguments | Result | Effects |
|
||||
|---|---|---|
|
||||
| std.io.reader.new(input: file<read>, capacity: usize, heap: cap<mem.alloc>) | NewResult: ok(Reader), err(NewError{file:file<read>,error:alloc_error}) | mem.alloc |
|
||||
| std.io.reader.read(input: mut<Reader>, destination: slice_mut<u8>) | ReadResult: ok(usize), err(io_error) | fs.read |
|
||||
| std.io.reader.read_exact(input: mut<Reader>, destination: slice_mut<u8>) | std.io.ReadExactResult | fs.read |
|
||||
| std.io.reader.finish(input: Reader) | FinishResult: ok(file<read>), unread(Reader) | none |
|
||||
| std.io.reader.discard(input: Reader) | Discarded{file:file<read>,unread:usize} | none |
|
||||
| std.io.writer.new(output: file<write>, capacity: usize, heap: cap<mem.alloc>) | NewResult: ok(Writer), err(NewError{file:file<write>,error:alloc_error}) | mem.alloc |
|
||||
| std.io.writer.write(output: mut<Writer>, bytes: slice<u8>) | WriteResult: ok, err(WriteError{accepted:usize,error:io_error}) | fs.write |
|
||||
| std.io.writer.flush(output: mut<Writer>) | FlushResult: ok, err(io_error) | fs.write |
|
||||
| std.io.writer.finish(output: Writer) | FinishResult: ok(file<write>), err(FinishError{writer:Writer,error:io_error}) | fs.write |
|
||||
| std.io.writer.discard(output: Writer) | Discarded{file:file<write>,pending:usize,failed:std.option.Option<io_error>} | none |
|
||||
|
||||
Move named files into new and named adapters into finish/discard. Constructor failure returns the exact original file before I/O; it can be closed or reused. Successful finish/discard drains/frees storage and refunds its recorded authority, returning the file for explicit use/close. No implicit growth, cleanup, retained caller borrow or extra authority is introduced.
|
||||
|
||||
Reader.read copies one cached prefix without refilling; when empty it refills the storage once, then returns a prefix. Thus read can return short even with more data available. Empty destination returns ok(0) without consuming/refilling and does not establish EOF. read_exact loops to fill the destination, using the std.io prefix/EOF/error contract. Reader.finish returns the complete unchanged Reader if cached unread bytes remain. Empty cache permits finish without probing EOF. Discard explicitly abandons unread prefetched bytes and returns their count: the file cursor remains advanced through prefetch, with no rewind. A read error retains ownership/offsets but promises no external cursor rollback or untouched attempted destination suffix.
|
||||
|
||||
Writer.write fills available storage and flushes a full buffer only when more input needs room. Ending with a full buffer keeps it pending. Empty input on a healthy writer does nothing. A write error reports bytes accepted from that call; accepted includes buffered data and is not an externally written/durable count. Pending data may include earlier calls. At capacity zero a direct failed write reports accepted=0 even if an external prefix was written. Healthy flush of empty storage performs no I/O; successful flush resets used to zero.
|
||||
|
||||
Any failed external write preserves pending bytes/used and records its first io_error. Every subsequent write, including empty, returns that error with accepted=0; every flush returns it, with no I/O or mutation. Finish returns the complete failed Writer plus error. There is no retry/reset because the failed operation may already have externally written an unknown prefix. Discard returns the file, pending count and failure state; pending can overlap that prefix and is not an exact loss count. Callers can explicitly handle the returned file according to their application protocol.
|
||||
|
||||
The visible representations are Reader{file,storage,begin,end}, 0≤begin≤end≤N, and Writer{file,storage,used,failed}, 0≤used≤N. Both buffers have initialized length=capacity=N. Caller field access/construction must preserve spec 16 §2.18's complete data/cursor/first-error invariant and must not independently consume/use the internal file/storage. Existing ownership, alias/task-loan, effects and Send/Share checks apply. Adapters can transfer to workers but cannot be shared immutably across tasks because files are non-Share. No unsafe/trusted boundary or new runtime/verifier state is needed.
|
||||
|
||||
The [copy example](../../examples/std/buffered_io.aji) uses two three-byte buffers (heap=mem.alloc:6), explicitly handles constructor/finish failure and returns the copied byte count, or zero after normal error cleanup. Conformance fixtures include capacities zero/full, unread finish/discard, exact EOF, allocation failure/refunds, worker transfer and rejected ownership/effect/alias cases. Hosted tests exercise read errors and real partial external writes; Linux raises the child file quota after failure to detect forbidden retry.
|
||||
@@ -0,0 +1,47 @@
|
||||
# RFC 0034: Owning bounded file reader and writer
|
||||
|
||||
Status: Proposed
|
||||
Scope: STD-7b, safe std.io.reader / std.io.writer after RFCs 0024/0033
|
||||
|
||||
## Motivation and normative integration
|
||||
|
||||
Spec 16 §2.18 adds concrete file adapters with fixed-capacity initialized byte buffers. They reduce repeated refill/flush code while keeping file ownership, allocation, effects, failure and cleanup explicit. Existing linear aggregates/buffers suffice without a reader trait, hidden destructor or new instruction.
|
||||
|
||||
## Preserved invariant
|
||||
|
||||
The adapter owns exactly one file and one initialized buffer. Reader offsets delimit its unread prefix; writer used count delimits pending bytes. A failed writer retains its first I/O error and prevents further writes/flush retries because failed external writes may have written an unknown prefix. All normal outcomes transfer complete resources or retain them in the adapter; no resource is implicitly closed.
|
||||
|
||||
## Verifier rule
|
||||
|
||||
No new abstract state/transfer function. Ordinary whole-value struct/enum moves, resource borrowing, buffer views, disjoint slices, effects/capabilities, alias/task loans, Copy/Send/Share and consumption checks apply. Fields remain visible with a documented caller invariant, as for existing safe collections. Invalid representation may trap through checked bounds/arithmetic but cannot bypass memory safety.
|
||||
|
||||
## Runtime / deterministic behavior
|
||||
|
||||
Only new allocates, with explicit mem.alloc authority, charging capacity bytes. A failed new returns the original unchanged file with alloc_error. Existing spec 11 I/O/blocking/fuel/profile behavior applies. Reader refill/cached copies and writer room-making flushes are ordinary safe source. Finish frees Copy storage and returns the file; unread/failed finish returns the complete adapter. Discard explicitly abandons buffered bytes, frees storage and returns the file plus counts/error state. No sync/close is hidden.
|
||||
|
||||
## Valid and rejected examples
|
||||
|
||||
The caller moves a file into new, explicitly consumes constructor failure, lends the adapter for reads/writes, handles finish returning the file or complete adapter, and closes the returned file. Discard reports unread/pending byte counts. Ignoring owning new/finish/discard outcomes, missing moves, invalid modes/effects, alias/task loans and sharing file-bearing adapters reject through existing diagnostics. The [copy example](../../examples/std/buffered_io.aji) consumes every constructor/finish result and closes returned files. [Unread finish](../../tests/conformance/source/buffered_io/unread_finish.aji) returns/reuses the complete reader; [explicit discard](../../tests/conformance/source/buffered_io/writer_discard.aji) reports abandoned pending bytes and refunds storage. [Ignoring a constructor failure](../../tests/conformance/source/buffered_io/new_error_leak.aji) and [ignoring failed finish](../../tests/conformance/source/buffered_io/finish_error_leak.aji) reject E3002. [Aliasing the cached bytes](../../tests/conformance/source/buffered_io/writer_alias.aji) rejects E2101; [sharing a file-bearing adapter](../../tests/conformance/source/buffered_io/not_share.aji) rejects E4002.
|
||||
|
||||
## Diagnostics / compatibility / AIR
|
||||
|
||||
No new diagnostic, syntax, AIR opcode/feature or verifier/runtime rule. Existing E7003/E7005/E3002/E2101/E4101/E3101/E4002 and resource availability checks apply. Two pinned modules are additive; old std.io APIs and AIR meanings retain their contracts. No implicit allocation, cleanup, authority, timeout, task or unsafe/trusted boundary.
|
||||
|
||||
## Agent complexity impact
|
||||
|
||||
Five operations per concrete adapter: reader new/read/read_exact/finish/discard; writer new/write/flush/finish/discard. Known file modes keep exact fs.read/fs.write effects. Writer failure state distinguishes acceptance into an owned buffer from confirmation of external output and prohibits an unsound automatic retry. No language/verifier state is added. Full representation/failure contracts stay on demand; measured Quick Reference/source budgets follow. Existing primitives suffice, and no empirical MCC improvement is claimed.
|
||||
|
||||
## Validation gate
|
||||
|
||||
Independent read/segmented-write byte oracles, capacities including zero, refill/flush/exact EOF, preserved guards, empty/full pending storage, constructor failure ownership/refunds, unread/failed finish, explicit discard, files transferred to tasks and restored loans, rejected ownership/type/effect/alias/Share cases. Hosted read error, externally partial write, no retry after error, source/AIR/package/import-order/profile/provenance checks, GC/scheduler stress and required repository/bundle checks. Paths and other system modules remain subsequent work.
|
||||
|
||||
|
||||
Measured cl100k growth from RFC 0033: Quick Reference 8,350 → 8,445 (+1.1%); other source documentation 58,473 → 61,223 (+4.7%); full source excluding AIR 66,823 → 69,668 (+4.3%). library/std.md is 24,364 tokens. Corresponding o200k groups are 8,478 / 61,408 / 69,886; qwen2.5 groups are 8,695 / 62,738 / 71,433. AIR remains 179,282 bytes / 19 files / 51 sections; its source-bundle copy remains 46,300 / 46,451 / 47,800 tokens. Cumulative cl100k growth over the pre-std 33,981-token baseline is +105.0%, justified for owner review by visible prefetch/cursor, acceptance/partial-write, first-error and explicit resource cleanup contracts. Quick Reference remains beyond the 4–6k target; front-door consolidation/selective reading remains separate work. This increment adds no language syntax, diagnostic or verifier/runtime state and claims no empirical MCC improvement.
|
||||
|
||||
## Validation result
|
||||
|
||||
All 34 buffered adapter conformance cases pass, including collection before every instruction. Owning reader/writer transfers and restored reader loans run 12 schedule seeds under quanta 1, 2, 7 and their own quantum. Independent Python byte oracles cover 48 complete read streams, 64 exact prefixes and 96 segmented writes over empty/exact/short inputs, zero and nonzero storage, multiple refills/flushes and preserved destination guards. Fixtures cover unread finish/discard cursor behavior, no EOF probe at finish, lazy full-cache flush, explicit discard, maximum capacity, constructor failure ownership and allocation refunds to narrowed authorities. Existing diagnostics reject missing moves, resource/result leaks, wrong mode/kind/effects, aliases, active task loans and non-Share references.
|
||||
|
||||
On Linux, 89 CLI checks pass: source/AIR/package/profile parity, reordered imports, pure audit and caller diagnostic provenance, a real file read fault (/proc/self/mem at unreadable offset zero), and four partial external write traces. A child-only three-byte file quota causes room-making, previous-call pending, explicit flush and capacity-zero direct write errors. The parent then raises the child's quota before the program attempts another write/empty write/flush/finish; the file remains exactly abc and the first error/pending bytes are preserved. This detects forbidden retry even when a later external write would succeed. Constructor/finish/discard failures retain complete owners, and the program explicitly closes returned files and verifies quota refunds. Tests use fuel and pipe handshakes, no wall-clock timeout or global host configuration change. macOS runs the portable checks/partial quota cases; read-fault and raised-quota retry probes are Linux-specific.
|
||||
|
||||
Unit/Quick Reference, formatting, hosted smoke, 115/115 source coverage constructs and reference generation pass. Full repository conformance passes with 1061 cases, zero failures and zero pending. The buffered file adapter gate is complete on this branch. Lexical paths and Std 0.1 remain required subsequent work.
|
||||
@@ -10,7 +10,7 @@ APIs SHOULD offer one canonical operation for a common need, small documentation
|
||||
|
||||
## 2. Bootstrap module registry
|
||||
|
||||
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.fmt`, `std.hash`, `std.hashmap`, `std.hashset`, `std.heap`, `std.io`, `std.map`, `std.math`, `std.option`, `std.result`, `std.utf8`, `std.vec` and `std.deque`. They are implemented in safe `.aji`; their stable source paths are `std/bytes.aji`, `std/encoding/base64.aji`, `std/encoding/hex.aji`, `std/fmt.aji`, `std/hash.aji`, `std/hashmap.aji`, `std/hashset.aji`, `std/heap.aji`, `std/io.aji`, `std/map.aji`, `std/math.aji`, `std/option.aji`, `std/result.aji`, `std/utf8.aji`, `std/vec.aji` and `std/deque.aji`, respectively. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. Modules other than std.io implement deterministic algorithms in every profile and have no concurrency effects; std.io follows the profile I/O/blocking rules in §2.17 and spec 11. Modules other than std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap and std.io declare effects [] and allocate no heap memory. std.io declares only the concrete I/O effects in §2.17 and allocates no heap storage. std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve declare mem.alloc and require explicit allocation authority (§2.10–§2.11, §2.13–§2.16); their other functions allocate no heap. Ordinary interpreter fuel limits apply.
|
||||
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.fmt`, `std.hash`, `std.hashmap`, `std.hashset`, `std.heap`, `std.io`, `std.io.reader`, `std.io.writer`, `std.map`, `std.math`, `std.option`, `std.result`, `std.utf8`, `std.vec` and `std.deque`. They are implemented in safe `.aji`; their stable source paths are `std/bytes.aji`, `std/encoding/base64.aji`, `std/encoding/hex.aji`, `std/fmt.aji`, `std/hash.aji`, `std/hashmap.aji`, `std/hashset.aji`, `std/heap.aji`, `std/io.aji`, `std/io/reader.aji`, `std/io/writer.aji`, `std/map.aji`, `std/math.aji`, `std/option.aji`, `std/result.aji`, `std/utf8.aji`, `std/vec.aji` and `std/deque.aji`, respectively. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. Modules other than std.io/std.io.reader/std.io.writer implement deterministic algorithms in every profile and have no concurrency effects; std.io/std.io.reader/std.io.writer follow the profile I/O/blocking rules in §2.17–§2.18 and spec 11. Modules other than std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap and std.io/std.io.reader/std.io.writer declare effects [] and allocate no heap memory. std.io declares only the concrete I/O effects in §2.17 and allocates no heap storage. std.io.reader/std.io.writer declare mem.alloc only on new and the concrete file effects in §2.18 on read/write/flush/finish; their other operations allocate no heap. std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve declare mem.alloc and require explicit allocation authority (§2.10–§2.11, §2.13–§2.16); their other functions allocate no heap. Ordinary interpreter fuel limits apply.
|
||||
|
||||
### 2.1 `std.math`
|
||||
|
||||
@@ -340,4 +340,35 @@ Copies use scratch chunks bounded by min(scratch.len, limit-completed). Successf
|
||||
|
||||
limit=0 MUST return limit(0) without I/O, including with empty scratch. Empty scratch with positive limit MUST return err(CopyError{read:0,confirmed_written:0,error:io_error.invalid_input}) without I/O. Scratch contents after the call are unspecified; bytes outside its slice MUST remain unchanged. Functions do not allocate, grow, sync or close; the caller explicitly supplies all grants, slices, limits and final resource consumption. Effects/mut/task-loan requirements apply even to no-op paths. Append copies use the existing append resource semantics.
|
||||
|
||||
Existing type, effect/capability, borrow/task-loan, resource availability, bounds and outcome checks apply without new diagnostics or verifier/runtime state. Operations use ordinary AIR and spec 11 profile behavior; no unsafe/trusted boundary, syntax/opcode/feature, implicit cleanup, timeout or existing semantic change is introduced. Blocking/fuel remain environment outcomes. Returned failures are explicit ordinary enums, never exceptions. Successful work is linear in read bytes plus intrinsic calls; caller capacities/limits and fuel bound work. Buffered adapters and lexical paths remain subsequent STD-7 increments.
|
||||
Existing type, effect/capability, borrow/task-loan, resource availability, bounds and outcome checks apply without new diagnostics or verifier/runtime state. Operations use ordinary AIR and spec 11 profile behavior; no unsafe/trusted boundary, syntax/opcode/feature, implicit cleanup, timeout or existing semantic change is introduced. Blocking/fuel remain environment outcomes. Returned failures are explicit ordinary enums, never exceptions. Successful work is linear in read bytes plus intrinsic calls; caller capacities/limits and fuel bound work. Buffered adapters are specified in §2.18; lexical paths remain a subsequent STD-7 increment.
|
||||
|
||||
### 2.18 `std.io.reader` / `std.io.writer`: owning bounded file adapters
|
||||
|
||||
The registry MUST include std.io.reader at std/io/reader.aji and std.io.writer at std/io/writer.aji, ordinary safe source with concrete file modes. They MUST NOT open/close files, sync, retain caller borrows, grow implicitly, infer effects or acquire authority through import. Each owns one file and one fixed-capacity initialized buffer<u8>. Only new declares mem.alloc and accepts explicit allocation authority; byte storage charges capacity bytes, including unused capacity. Capacity zero is valid and uses direct primitive reads/writes. Constructor failure MUST return the exact original file with alloc_error, without I/O or resource consumption.
|
||||
|
||||
Reader is `{ file: file<read>, storage: buffer<u8>, begin: usize, end: usize }`, with fully initialized length=capacity=N and 0≤begin≤end≤N. Bytes begin..end are unread prefetched data; the underlying file cursor has advanced through those bytes. Writer is `{ file: file<write>, storage: buffer<u8>, used: usize, failed: std.option.Option<io_error> }`, with fully initialized length=capacity=N and 0≤used≤N. Bytes 0..used are pending accepted data; failed is none or the first failed external-write error. Neither adapter's file/storage may be independently used, consumed or mutated in a way that violates these logical invariants. Ordinary visible fields grant no representation privilege.
|
||||
|
||||
Reader types: NewError{file:file<read>,error:alloc_error}; NewResult{ok(Reader),err(NewError)}; ReadResult{ok(usize),err(io_error)}; FinishResult{ok(file<read>),unread(Reader)}; Discarded{file:file<read>,unread:usize}. Writer types: NewError{file:file<write>,error:alloc_error}; NewResult{ok(Writer),err(NewError)}; WriteError{accepted:usize,error:io_error}; WriteResult{ok,err(WriteError)}; FlushResult{ok,err(io_error)}; FinishError{writer:Writer,error:io_error}; FinishResult{ok(file<write>),err(FinishError)}; Discarded{file:file<write>,pending:usize,failed:std.option.Option<io_error>}. Every file/adapter-bearing result is linear and MUST be explicitly consumed.
|
||||
|
||||
| Module operation | Result | Effects |
|
||||
|---|---|---|
|
||||
| reader.new(input: file<read>, capacity: usize, heap: cap<mem.alloc>) | NewResult | mem.alloc |
|
||||
| reader.read(input: mut<Reader>, destination: slice_mut<u8>) | ReadResult | fs.read |
|
||||
| reader.read_exact(input: mut<Reader>, destination: slice_mut<u8>) | std.io.ReadExactResult | fs.read |
|
||||
| reader.finish(input: Reader) | FinishResult | none |
|
||||
| reader.discard(input: Reader) | Discarded | none |
|
||||
| writer.new(output: file<write>, capacity: usize, heap: cap<mem.alloc>) | NewResult | mem.alloc |
|
||||
| writer.write(output: mut<Writer>, bytes: slice<u8>) | WriteResult | fs.write |
|
||||
| writer.flush(output: mut<Writer>) | FlushResult | fs.write |
|
||||
| writer.finish(output: Writer) | FinishResult | fs.write |
|
||||
| writer.discard(output: Writer) | Discarded | none |
|
||||
|
||||
Reader.read returns at most destination.len bytes. Empty destination returns ok(0) without I/O or consuming cached data; it does not establish EOF. If cached bytes exist, copy min(unread,destination.len) and advance begin, without refilling during that call. Otherwise, for N>0, refill the whole storage using fs.read, then return one cached prefix; a successful zero refill returns ok(0) as EOF. With N=0 delegate one primitive read directly to the destination. Refill errors retain the file/storage and existing offsets, returning the original io_error with no guessed external cursor rollback. Bytes outside the destination remain unchanged. read_exact composes read over destination suffixes and uses §2.17's completed-prefix/EOF/error contract, with no borrow escaping.
|
||||
|
||||
Reader.finish MUST return unread(the complete unchanged Reader) when begin<end. Otherwise drain/free Copy storage and return the original file at its current cursor. It does not require a confirmed EOF and does not read or close. Reader.discard explicitly abandons end-begin prefetched bytes, drains/frees storage and returns the file plus unread count. No rewind is promised; the underlying cursor remains advanced through prefetch. Storage refunds follow its recorded allocation authority.
|
||||
|
||||
Writer.write accepts the complete input into buffered storage or successful writes, returning ok; accepted is not a durability/external-write count. With N>0, copy as much as fits, flushing a full buffer only to make room for further input. If a call ends with a full buffer, retain it until a later write/flush/finish. On a room-making flush error, return WriteError with the number of this call's input bytes already accepted and the original error; the unaccepted suffix remains caller-owned. Buffered pending bytes remain in Writer, including accepted bytes from earlier calls. With N=0, delegate fs.write_all directly: failure has accepted=0, though an external prefix MAY have been written. No count exceeds input.len or overflows.
|
||||
|
||||
Writer.flush on a healthy empty buffer returns ok without I/O. On a successful external write, used becomes zero. On any external write failure, used/data remain unchanged and failed becomes some(the first error); no automatic retry/reset is allowed. Once failed is some(error), every write (including empty) MUST return err with accepted=0 and that error, and every flush MUST return err(error), without I/O or buffer mutation. Unknown partial external writes make retrying pending data unsound. Writer.finish first flushes: success drains/frees storage and returns the file; error returns FinishError with the complete unchanged failed Writer and original error. Finish does not sync or close. Writer.discard explicitly abandons buffered pending bytes, drains/frees storage and returns the file, used count and failure state; pending on failure MAY overlap an already externally written prefix and is not an exact loss count.
|
||||
|
||||
Effects, moves and mut/task-loan requirements apply on no-op/cached/failure paths. Existing aggregate/buffer moves, resource borrowing, alias exclusion, bounds/arithmetic, effect/capability and Copy/Send/Share rules verify all safe code; files make adapters non-Share and ordinary Send checks govern task transfer. Deterministic/hosted behavior follows existing profile primitives. No new AIR meaning, instruction/feature, syntax, diagnostic, verifier/runtime state, unsafe boundary, implicit destructor or cancellation policy is introduced. Invalid representation may trap through existing checked operations, preserving memory safety. Valid bookkeeping does not trap for a valid invariant/conforming primitives. Work is linear in initialized/copy/drained bytes plus I/O calls, with existing interpreter prefix-copy/GC costs. Lexical paths and Std 0.1 remain unfinished.
|
||||
@@ -0,0 +1,46 @@
|
||||
module demo.buffered_io
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
|
||||
fn main(src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.read,fs.write,mem.alloc] {
|
||||
let input = fs.open_read(src,b"/in/data") or trap;
|
||||
let output = fs.create(dst,b"/out/data") or trap;
|
||||
let reader = match std.io.reader.new(move input,3usize,heap) {
|
||||
std.io.reader.NewResult::ok(value) => { move value },
|
||||
std.io.reader.NewResult::err(error) => {
|
||||
let (file,error) = struct.unpack(move error);
|
||||
fs.close(move file) or trap; fs.close(move output) or trap; return 0;
|
||||
}
|
||||
};
|
||||
let writer = match std.io.writer.new(move output,3usize,heap) {
|
||||
std.io.writer.NewResult::ok(value) => { move value },
|
||||
std.io.writer.NewResult::err(error) => {
|
||||
let (file,error) = struct.unpack(move error);
|
||||
fs.close(move file) or trap;
|
||||
let (file,unread) = struct.unpack(std.io.reader.discard(move reader)); fs.close(move file) or trap; return 0;
|
||||
}
|
||||
};
|
||||
var failed: bool = false; var count: usize = 0; var bytes: [u8;2] = [0u8;2];
|
||||
{ mut input = reader; mut output = writer; loop {
|
||||
match std.io.reader.read(input,mut bytes) {
|
||||
std.io.reader.ReadResult::err(error) => { failed = true; break; },
|
||||
std.io.reader.ReadResult::ok(n) => {
|
||||
if n == 0usize { break; }
|
||||
match std.io.writer.write(output,ref bytes[0usize..n]) {
|
||||
std.io.writer.WriteResult::ok => { count = count + n; },
|
||||
std.io.writer.WriteResult::err(error) => { failed = true; break; }
|
||||
}
|
||||
}
|
||||
}
|
||||
} }
|
||||
let (file,unread) = struct.unpack(std.io.reader.discard(move reader)); fs.close(move file) or trap;
|
||||
match std.io.writer.finish(move writer) {
|
||||
std.io.writer.FinishResult::ok(file) => { fs.close(move file) or trap; },
|
||||
std.io.writer.FinishResult::err(error) => {
|
||||
let (writer,error) = struct.unpack(move error);
|
||||
let (file,pending,error) = struct.unpack(std.io.writer.discard(move writer));
|
||||
fs.close(move file) or trap; return 0;
|
||||
}
|
||||
}
|
||||
if failed { return 0; } return count as! u64;
|
||||
}
|
||||
@@ -13,6 +13,8 @@ pub const registry = [_]Source{
|
||||
.{ .name = "std.hashset", .file = "std/hashset.aji", .source = @embedFile("std_hashset_aji") },
|
||||
.{ .name = "std.heap", .file = "std/heap.aji", .source = @embedFile("std_heap_aji") },
|
||||
.{ .name = "std.io", .file = "std/io.aji", .source = @embedFile("std_io_aji") },
|
||||
.{ .name = "std.io.reader", .file = "std/io/reader.aji", .source = @embedFile("std_io_reader_aji") },
|
||||
.{ .name = "std.io.writer", .file = "std/io/writer.aji", .source = @embedFile("std_io_writer_aji") },
|
||||
.{ .name = "std.map", .file = "std/map.aji", .source = @embedFile("std_map_aji") },
|
||||
.{ .name = "std.math", .file = "std/math.aji", .source = @embedFile("std_math_aji") },
|
||||
.{ .name = "std.option", .file = "std/option.aji", .source = @embedFile("std_option_aji") },
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
module std.io.reader
|
||||
import std.io;
|
||||
|
||||
struct Reader { file: file<read>, storage: buffer<u8>, begin: usize, end: usize }
|
||||
struct NewError { file: file<read>, error: alloc_error }
|
||||
enum NewResult { ok(Reader), err(NewError) }
|
||||
enum ReadResult { ok(usize), err(io_error) }
|
||||
enum FinishResult { ok(file<read>), unread(Reader) }
|
||||
struct Discarded { file: file<read>, unread: usize }
|
||||
|
||||
fn new(input: file<read>,capacity: usize,heap: cap<mem.alloc>) -> NewResult effects [mem.alloc] {
|
||||
match buffer.new<u8>(capacity,heap) {
|
||||
err(error) => { return NewResult::err(NewError { file: move input, error: error }); }
|
||||
ok(storage) => {
|
||||
{ mut output = storage; for i in 0usize..capacity { buffer.push(output,0u8) or trap; } }
|
||||
return NewResult::ok(Reader { file: move input, storage: move storage, begin: 0usize, end: 0usize });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read(input: mut<Reader>,destination: slice_mut<u8>) -> ReadResult effects [fs.read] {
|
||||
if destination.len == 0usize { return ReadResult::ok(0usize); }
|
||||
var capacity: usize = 0;
|
||||
{ ref storage = input.storage; capacity = buffer.len(storage); }
|
||||
if capacity == 0usize {
|
||||
mut file = input.file;
|
||||
match fs.read(file,destination) { ok(count) => { return ReadResult::ok(count); }, err(error) => { return ReadResult::err(error); } }
|
||||
}
|
||||
if input.begin == input.end {
|
||||
var count: usize = 0;
|
||||
{ mut file = input.file; mut storage = input.storage; mut bytes = buffer.view(storage);
|
||||
match fs.read(file,bytes) { ok(read) => { assert(read <= capacity); count = read; }, err(error) => { return ReadResult::err(error); } }
|
||||
}
|
||||
input.begin = 0usize; input.end = count;
|
||||
if count == 0usize { return ReadResult::ok(0usize); }
|
||||
}
|
||||
let available = input.end - input.begin;
|
||||
let count = if destination.len < available { destination.len } else { available };
|
||||
{ ref storage = input.storage; ref bytes = buffer.view(storage);
|
||||
for i in 0usize..count { destination[i] = bytes[input.begin + i]; }
|
||||
}
|
||||
input.begin = input.begin + count; return ReadResult::ok(count);
|
||||
}
|
||||
|
||||
fn read_exact(input: mut<Reader>,destination: slice_mut<u8>) -> std.io.ReadExactResult effects [fs.read] {
|
||||
var completed: usize = 0;
|
||||
while completed < destination.len {
|
||||
mut suffix = destination[completed..destination.len];
|
||||
match read(input,suffix) {
|
||||
ReadResult::ok(count) => { assert(count <= suffix.len); if count == 0usize { return std.io.ReadExactResult::eof(completed); } completed = completed + count; }
|
||||
ReadResult::err(error) => { return std.io.ReadExactResult::err(std.io.ReadError { completed: completed, error: error }); }
|
||||
}
|
||||
}
|
||||
return std.io.ReadExactResult::ok;
|
||||
}
|
||||
|
||||
fn finish(input: Reader) -> FinishResult effects [] {
|
||||
if input.begin < input.end { return FinishResult::unread(move input); }
|
||||
let (file,storage,begin,end) = struct.unpack(move input);
|
||||
{ mut bytes = storage; while buffer.len(bytes) != 0usize { let unused = buffer.pop(bytes) or trap; } }
|
||||
buffer.free(move storage) or trap; return FinishResult::ok(move file);
|
||||
}
|
||||
|
||||
fn discard(input: Reader) -> Discarded effects [] {
|
||||
let (file,storage,begin,end) = struct.unpack(move input);
|
||||
let unread = end - begin;
|
||||
{ mut bytes = storage; while buffer.len(bytes) != 0usize { let unused = buffer.pop(bytes) or trap; } }
|
||||
buffer.free(move storage) or trap; return Discarded { file: move file, unread: unread };
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
module std.io.writer
|
||||
import std.option;
|
||||
|
||||
struct Writer { file: file<write>, storage: buffer<u8>, used: usize, failed: std.option.Option<io_error> }
|
||||
struct NewError { file: file<write>, error: alloc_error }
|
||||
enum NewResult { ok(Writer), err(NewError) }
|
||||
struct WriteError { accepted: usize, error: io_error }
|
||||
enum WriteResult { ok, err(WriteError) }
|
||||
enum FlushResult { ok, err(io_error) }
|
||||
struct FinishError { writer: Writer, error: io_error }
|
||||
enum FinishResult { ok(file<write>), err(FinishError) }
|
||||
struct Discarded { file: file<write>, pending: usize, failed: std.option.Option<io_error> }
|
||||
|
||||
fn new(output: file<write>,capacity: usize,heap: cap<mem.alloc>) -> NewResult effects [mem.alloc] {
|
||||
match buffer.new<u8>(capacity,heap) {
|
||||
err(error) => { return NewResult::err(NewError { file: move output, error: error }); }
|
||||
ok(storage) => {
|
||||
{ mut bytes = storage; for i in 0usize..capacity { buffer.push(bytes,0u8) or trap; } }
|
||||
return NewResult::ok(Writer { file: move output, storage: move storage, used: 0usize, failed: std.option.Option::<io_error>::none });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write(output: mut<Writer>,bytes: slice<u8>) -> WriteResult effects [fs.write] {
|
||||
match output.failed { std.option.Option::<io_error>::some(error) => { return WriteResult::err(WriteError { accepted: 0usize, error: error }); }, std.option.Option::<io_error>::none => {} }
|
||||
if bytes.len == 0usize { return WriteResult::ok; }
|
||||
var capacity: usize = 0;
|
||||
{ ref storage = output.storage; capacity = buffer.len(storage); }
|
||||
if capacity == 0usize {
|
||||
var error_value: io_error = io_error::other; var failed: bool = false;
|
||||
{ mut file = output.file; match fs.write_all(file,bytes) { ok() => {}, err(error) => { error_value = error; failed = true; } } }
|
||||
if failed { output.failed = std.option.Option::<io_error>::some(error_value); return WriteResult::err(WriteError { accepted: 0usize, error: error_value }); }
|
||||
return WriteResult::ok;
|
||||
}
|
||||
var accepted: usize = 0;
|
||||
while accepted < bytes.len {
|
||||
if output.used == capacity {
|
||||
match flush(output) { FlushResult::ok => {}, FlushResult::err(error) => { return WriteResult::err(WriteError { accepted: accepted, error: error }); } }
|
||||
}
|
||||
let available = capacity - output.used; let remaining = bytes.len - accepted;
|
||||
let count = if available < remaining { available } else { remaining };
|
||||
{ mut storage = output.storage; mut target = buffer.view(storage);
|
||||
for i in 0usize..count { target[output.used + i] = bytes[accepted + i]; }
|
||||
}
|
||||
output.used = output.used + count; accepted = accepted + count;
|
||||
}
|
||||
return WriteResult::ok;
|
||||
}
|
||||
|
||||
fn flush(output: mut<Writer>) -> FlushResult effects [fs.write] {
|
||||
match output.failed { std.option.Option::<io_error>::some(error) => { return FlushResult::err(error); }, std.option.Option::<io_error>::none => {} }
|
||||
if output.used == 0usize { return FlushResult::ok; }
|
||||
var error_value: io_error = io_error::other; var failed: bool = false;
|
||||
{ mut file = output.file; ref storage = output.storage; ref bytes = buffer.view(storage); ref pending = bytes[0usize..output.used];
|
||||
match fs.write_all(file,pending) { ok() => {}, err(error) => { error_value = error; failed = true; } }
|
||||
}
|
||||
if failed { output.failed = std.option.Option::<io_error>::some(error_value); return FlushResult::err(error_value); }
|
||||
output.used = 0usize; return FlushResult::ok;
|
||||
}
|
||||
|
||||
fn finish(output: Writer) -> FinishResult effects [fs.write] {
|
||||
var error_value: io_error = io_error::other; var failure: bool = false;
|
||||
{ mut value = output; match flush(value) { FlushResult::ok => {}, FlushResult::err(error) => { error_value = error; failure = true; } } }
|
||||
if failure { return FinishResult::err(FinishError { writer: move output, error: error_value }); }
|
||||
let (file,storage,used,failed) = struct.unpack(move output);
|
||||
{ mut bytes = storage; while buffer.len(bytes) != 0usize { let unused = buffer.pop(bytes) or trap; } }
|
||||
buffer.free(move storage) or trap; return FinishResult::ok(move file);
|
||||
}
|
||||
|
||||
fn discard(output: Writer) -> Discarded effects [] {
|
||||
let (file,storage,used,failed) = struct.unpack(move output);
|
||||
{ mut bytes = storage; while buffer.len(bytes) != 0usize { let unused = buffer.pop(bytes) or trap; } }
|
||||
buffer.free(move storage) or trap; return Discarded { file: move file, pending: used, failed: failed };
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E3002
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(q: std.io.writer.Writer) -> void effects [] { std.io.writer.discard(move q); }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,34 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap; let value = reader(move file,3usize,heap); var byte: [u8;1] = [0u8;1];
|
||||
{ mut q = value; match std.io.reader.read(q,mut byte) { std.io.reader.ReadResult::ok(n) => {}, std.io.reader.ReadResult::err(error) => { trap; } }
|
||||
exact(std.io.reader.read_exact(q,mut byte[0usize..0usize])); match std.io.reader.read(q,mut byte[0usize..0usize]) { std.io.reader.ReadResult::ok(n) => { assert(n == 0usize); }, std.io.reader.ReadResult::err(error) => { trap; } } }
|
||||
let (file,unread) = struct.unpack(std.io.reader.discard(move value)); assert(unread == 2usize); fs.close(move file) or trap; return 42;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap; let value = reader(move file,3usize,heap); var byte: [u8;1] = [0u8;1];
|
||||
{ mut q = value; exact(std.io.reader.read_exact(q,mut byte[0usize..0usize])); match std.io.reader.read(q,mut byte[0usize..0usize]) { std.io.reader.ReadResult::ok(n) => { assert(n == 0usize); }, std.io.reader.ReadResult::err(error) => { trap; } } }
|
||||
let file = finished_reader(move value); exact(std.io.read_exact_file(mut file,mut byte)); assert(byte[0] == 97u8); fs.close(move file) or trap; return 42;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E2101
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(q: std.io.reader.Reader) -> void effects [fs.read] { mut file = q.file; std.io.reader.finish(move q); var bytes: [u8;1] = [0u8;1]; fs.read(file,mut bytes) or trap; trap; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E3002
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(q: std.io.writer.Writer) -> void effects [fs.write] { match std.io.writer.finish(move q) { std.io.writer.FinishResult::ok(file) => { trap; }, std.io.writer.FinishResult::err(error) => { return; } } }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,33 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap; let value = reader(move file,3usize,heap); var prefix: [u8;3] = [0u8;3];
|
||||
{ mut q = value; exact(std.io.reader.read_exact(q,mut prefix)); }
|
||||
let file = finished_reader(move value); exact(std.io.read_exact_file(mut file,mut prefix)); assert(prefix[0] == 100u8); assert(prefix[2] == 102u8); fs.close(move file) or trap; return 42;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: dst=fs.write:/out
|
||||
// grant: heap=mem.alloc:3
|
||||
// grant: src=fs.read:/out
|
||||
// file: /out/data=abcd
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn observed(src: cap<fs.read>,expected: usize) -> void effects [fs.read] {
|
||||
let file = fs.open_read(src,b"/out/data") or trap; var bytes: [u8;6] = [0u8;6]; match std.io.read_to_end_file(mut file,mut bytes) { std.io.ReadToEndResult::eof(n) => { assert(n == expected); }, std.io.ReadToEndResult::full(n) => { trap; }, std.io.ReadToEndResult::err(error) => { trap; } } fs.close(move file) or trap;
|
||||
}
|
||||
fn main(src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.read,fs.write,mem.alloc] {
|
||||
let file = fs.create(dst,b"/out/data") or trap; let value = writer(move file,3usize,heap); let blank: [u8;1] = [0u8;1];
|
||||
{ mut q = value; written(std.io.writer.write(q,b"abc")); written(std.io.writer.write(q,ref blank[0usize..0usize])); } observed(src,0usize);
|
||||
{ mut q = value; written(std.io.writer.write(q,b"d")); } observed(src,3usize);
|
||||
{ mut q = value; match std.io.writer.flush(q) { std.io.writer.FlushResult::ok => {}, std.io.writer.FlushResult::err(error) => { trap; } } match std.io.writer.flush(q) { std.io.writer.FlushResult::ok => {}, std.io.writer.FlushResult::err(error) => { trap; } } } observed(src,4usize);
|
||||
fs.close(finished_writer(move value)) or trap; return 42;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// expect: reject E4101
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn worker(q: mut<std.io.reader.Reader>) -> void effects [fs.read] {}
|
||||
fn bad(q: mut<std.io.reader.Reader>) -> void effects [fs.read,task.spawn,task.wait] { scope s { let t = task.spawn(s,worker,q); var bytes: [u8;1] = [0u8;1]; std.io.reader.read(q,mut bytes); task.join(move t); } }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,35 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
// schedules: 12
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn worker(q: mut<std.io.reader.Reader>) -> void effects [fs.read] { var bytes: [u8;3] = [0u8;3]; exact(std.io.reader.read_exact(q,mut bytes)); assert(bytes[0] == 97u8); }
|
||||
fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc,task.spawn,task.wait] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap; let value = reader(move file,3usize,heap); var bytes: [u8;3] = [0u8;3];
|
||||
{ mut q = value; scope s { let t = task.spawn(s,worker,q); task.join(move t); exact(std.io.reader.read_exact(q,mut bytes)); } }
|
||||
assert(bytes[0] == 100u8); fs.close(finished_reader(move value)) or trap; return 42;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
// grant: dst=fs.write:/out
|
||||
// file: /out/data=
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.read,fs.write,mem.alloc] {
|
||||
let input = fs.open_read(src,b"/in/data") or trap; let output = fs.create(dst,b"/out/data") or trap;
|
||||
match std.io.reader.new(move input,18446744073709551615usize,heap) { std.io.reader.NewResult::ok(value) => { trap; }, std.io.reader.NewResult::err(error) => { let (file,error) = struct.unpack(move error); fs.close(move file) or trap; } }
|
||||
match std.io.writer.new(move output,18446744073709551615usize,heap) { std.io.writer.NewResult::ok(value) => { trap; }, std.io.writer.NewResult::err(error) => { let (file,error) = struct.unpack(move error); fs.close(move file) or trap; } }
|
||||
return 42;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E3101
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(file: file<read>,heap: cap<mem.alloc>) -> std.io.reader.NewResult effects [] { return std.io.reader.new(move file,3usize,heap); }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E7003
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(q: std.io.reader.Reader) -> void effects [] { std.io.reader.finish(q); trap; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E3101
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(q: mut<std.io.reader.Reader>,dest: slice_mut<u8>) -> void effects [] { std.io.reader.read(q,dest); }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E3101
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(q: mut<std.io.writer.Writer>) -> void effects [] { std.io.writer.flush(q); }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,284 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/case
|
||||
// grant: dst=fs.write:/case
|
||||
// grant: heap=mem.alloc:5
|
||||
// file: /case/input=abcdefgh
|
||||
// file: /case/output=abcdefgh
|
||||
// fuel: 2000000
|
||||
module test.buffered_model
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.bytes;
|
||||
fn prepare(contents: slice<u8>,dst: cap<fs.write>) -> void effects [fs.write] {
|
||||
let file = fs.create(dst,b"/case/input") or trap; fs.write_all(file,contents) or trap; fs.close(move file) or trap;
|
||||
}
|
||||
fn reads(contents: slice<u8>,capacity: usize,chunk: usize,src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> void effects [fs.read,fs.write,mem.alloc] {
|
||||
prepare(contents,dst); let file = fs.open_read(src,b"/case/input") or trap;
|
||||
match std.io.reader.new(move file,capacity,heap) {
|
||||
std.io.reader.NewResult::err(error) => { trap; }, std.io.reader.NewResult::ok(value) => {
|
||||
var bytes: [u8;9] = [255u8;9]; var offset: usize = 0;
|
||||
{ mut q = value; loop {
|
||||
match std.io.reader.read(q,mut bytes[1usize..chunk+1usize]) {
|
||||
std.io.reader.ReadResult::err(error) => { trap; }, std.io.reader.ReadResult::ok(count) => {
|
||||
assert(count <= chunk); assert(offset + count <= contents.len);
|
||||
assert(std.bytes.equal(ref bytes[1usize..count+1usize],ref contents[offset..offset+count]));
|
||||
assert(bytes[0] == 255u8); for i in chunk+1usize..9usize { assert(bytes[i] == 255u8); }
|
||||
offset = offset + count; if count == 0usize { break; }
|
||||
}
|
||||
}
|
||||
} }
|
||||
assert(offset == contents.len);
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { fs.close(move file) or trap; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
}
|
||||
}
|
||||
fn exact(contents: slice<u8>,capacity: usize,requested: usize,prefix: usize,full: bool,src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> void effects [fs.read,fs.write,mem.alloc] {
|
||||
prepare(contents,dst); let file = fs.open_read(src,b"/case/input") or trap;
|
||||
match std.io.reader.new(move file,capacity,heap) {
|
||||
std.io.reader.NewResult::err(error) => { trap; }, std.io.reader.NewResult::ok(value) => {
|
||||
var bytes: [u8;12] = [255u8;12];
|
||||
{ mut q = value; match std.io.reader.read_exact(q,mut bytes[1usize..requested+1usize]) {
|
||||
std.io.ReadExactResult::ok => { assert(full); }, std.io.ReadExactResult::eof(n) => { assert(not full); assert(n == prefix); }, std.io.ReadExactResult::err(error) => { trap; }
|
||||
} }
|
||||
assert(std.bytes.equal(ref bytes[1usize..prefix+1usize],ref contents[0usize..prefix]));
|
||||
assert(bytes[0] == 255u8); for i in prefix+1usize..12usize { assert(bytes[i] == 255u8); }
|
||||
let (file,unread) = struct.unpack(std.io.reader.discard(move value)); fs.close(move file) or trap;
|
||||
}
|
||||
}
|
||||
}
|
||||
fn writes(contents: slice<u8>,capacity: usize,chunk: usize,flush: bool,src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> void effects [fs.read,fs.write,mem.alloc] {
|
||||
let file = fs.create(dst,b"/case/output") or trap;
|
||||
match std.io.writer.new(move file,capacity,heap) {
|
||||
std.io.writer.NewResult::err(error) => { trap; }, std.io.writer.NewResult::ok(value) => {
|
||||
var offset: usize = 0;
|
||||
{ mut q = value; match std.io.writer.write(q,ref contents[0usize..0usize]) { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } }
|
||||
while offset < contents.len {
|
||||
let left = contents.len - offset; let count = if left < chunk { left } else { chunk };
|
||||
match std.io.writer.write(q,ref contents[offset..offset+count]) { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } }
|
||||
offset = offset + count;
|
||||
if flush { match std.io.writer.flush(q) { std.io.writer.FlushResult::ok => {}, std.io.writer.FlushResult::err(error) => { trap; } } }
|
||||
}
|
||||
}
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { fs.close(move file) or trap; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
}
|
||||
let file = fs.open_read(src,b"/case/output") or trap; var bytes: [u8;12] = [255u8;12];
|
||||
match std.io.read_to_end_file(mut file,mut bytes[1usize..11usize]) { std.io.ReadToEndResult::eof(n) => { assert(n == contents.len); assert(std.bytes.equal(ref bytes[1usize..n+1usize],contents)); }, std.io.ReadToEndResult::full(n) => { trap; }, std.io.ReadToEndResult::err(error) => { trap; } }
|
||||
assert(bytes[0] == 255u8); assert(bytes[11] == 255u8); fs.close(move file) or trap;
|
||||
}
|
||||
fn main(src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.read,fs.write,mem.alloc] {
|
||||
let blank: [u8;1] = [0u8;1];
|
||||
reads(ref blank[0usize..0usize],0usize,1usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],0usize,1usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],0usize,1usize,true,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],0usize,3usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],0usize,3usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],0usize,3usize,true,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],0usize,7usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],0usize,7usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],0usize,7usize,true,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],0usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],0usize,1usize,0usize,false,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],0usize,4usize,0usize,false,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],0usize,9usize,0usize,false,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],1usize,1usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],1usize,1usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],1usize,1usize,true,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],1usize,3usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],1usize,3usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],1usize,3usize,true,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],1usize,7usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],1usize,7usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],1usize,7usize,true,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],1usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],1usize,1usize,0usize,false,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],1usize,4usize,0usize,false,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],1usize,9usize,0usize,false,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],2usize,1usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],2usize,1usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],2usize,1usize,true,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],2usize,3usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],2usize,3usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],2usize,3usize,true,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],2usize,7usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],2usize,7usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],2usize,7usize,true,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],2usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],2usize,1usize,0usize,false,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],2usize,4usize,0usize,false,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],2usize,9usize,0usize,false,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],5usize,1usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],5usize,1usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],5usize,1usize,true,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],5usize,3usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],5usize,3usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],5usize,3usize,true,src,dst,heap);
|
||||
reads(ref blank[0usize..0usize],5usize,7usize,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],5usize,7usize,false,src,dst,heap);
|
||||
writes(ref blank[0usize..0usize],5usize,7usize,true,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],5usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],5usize,1usize,0usize,false,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],5usize,4usize,0usize,false,src,dst,heap);
|
||||
exact(ref blank[0usize..0usize],5usize,9usize,0usize,false,src,dst,heap);
|
||||
reads(b"a",0usize,1usize,src,dst,heap);
|
||||
writes(b"a",0usize,1usize,false,src,dst,heap);
|
||||
writes(b"a",0usize,1usize,true,src,dst,heap);
|
||||
reads(b"a",0usize,3usize,src,dst,heap);
|
||||
writes(b"a",0usize,3usize,false,src,dst,heap);
|
||||
writes(b"a",0usize,3usize,true,src,dst,heap);
|
||||
reads(b"a",0usize,7usize,src,dst,heap);
|
||||
writes(b"a",0usize,7usize,false,src,dst,heap);
|
||||
writes(b"a",0usize,7usize,true,src,dst,heap);
|
||||
exact(b"a",0usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"a",0usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"a",0usize,4usize,1usize,false,src,dst,heap);
|
||||
exact(b"a",0usize,9usize,1usize,false,src,dst,heap);
|
||||
reads(b"a",1usize,1usize,src,dst,heap);
|
||||
writes(b"a",1usize,1usize,false,src,dst,heap);
|
||||
writes(b"a",1usize,1usize,true,src,dst,heap);
|
||||
reads(b"a",1usize,3usize,src,dst,heap);
|
||||
writes(b"a",1usize,3usize,false,src,dst,heap);
|
||||
writes(b"a",1usize,3usize,true,src,dst,heap);
|
||||
reads(b"a",1usize,7usize,src,dst,heap);
|
||||
writes(b"a",1usize,7usize,false,src,dst,heap);
|
||||
writes(b"a",1usize,7usize,true,src,dst,heap);
|
||||
exact(b"a",1usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"a",1usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"a",1usize,4usize,1usize,false,src,dst,heap);
|
||||
exact(b"a",1usize,9usize,1usize,false,src,dst,heap);
|
||||
reads(b"a",2usize,1usize,src,dst,heap);
|
||||
writes(b"a",2usize,1usize,false,src,dst,heap);
|
||||
writes(b"a",2usize,1usize,true,src,dst,heap);
|
||||
reads(b"a",2usize,3usize,src,dst,heap);
|
||||
writes(b"a",2usize,3usize,false,src,dst,heap);
|
||||
writes(b"a",2usize,3usize,true,src,dst,heap);
|
||||
reads(b"a",2usize,7usize,src,dst,heap);
|
||||
writes(b"a",2usize,7usize,false,src,dst,heap);
|
||||
writes(b"a",2usize,7usize,true,src,dst,heap);
|
||||
exact(b"a",2usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"a",2usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"a",2usize,4usize,1usize,false,src,dst,heap);
|
||||
exact(b"a",2usize,9usize,1usize,false,src,dst,heap);
|
||||
reads(b"a",5usize,1usize,src,dst,heap);
|
||||
writes(b"a",5usize,1usize,false,src,dst,heap);
|
||||
writes(b"a",5usize,1usize,true,src,dst,heap);
|
||||
reads(b"a",5usize,3usize,src,dst,heap);
|
||||
writes(b"a",5usize,3usize,false,src,dst,heap);
|
||||
writes(b"a",5usize,3usize,true,src,dst,heap);
|
||||
reads(b"a",5usize,7usize,src,dst,heap);
|
||||
writes(b"a",5usize,7usize,false,src,dst,heap);
|
||||
writes(b"a",5usize,7usize,true,src,dst,heap);
|
||||
exact(b"a",5usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"a",5usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"a",5usize,4usize,1usize,false,src,dst,heap);
|
||||
exact(b"a",5usize,9usize,1usize,false,src,dst,heap);
|
||||
reads(b"abc",0usize,1usize,src,dst,heap);
|
||||
writes(b"abc",0usize,1usize,false,src,dst,heap);
|
||||
writes(b"abc",0usize,1usize,true,src,dst,heap);
|
||||
reads(b"abc",0usize,3usize,src,dst,heap);
|
||||
writes(b"abc",0usize,3usize,false,src,dst,heap);
|
||||
writes(b"abc",0usize,3usize,true,src,dst,heap);
|
||||
reads(b"abc",0usize,7usize,src,dst,heap);
|
||||
writes(b"abc",0usize,7usize,false,src,dst,heap);
|
||||
writes(b"abc",0usize,7usize,true,src,dst,heap);
|
||||
exact(b"abc",0usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"abc",0usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"abc",0usize,4usize,3usize,false,src,dst,heap);
|
||||
exact(b"abc",0usize,9usize,3usize,false,src,dst,heap);
|
||||
reads(b"abc",1usize,1usize,src,dst,heap);
|
||||
writes(b"abc",1usize,1usize,false,src,dst,heap);
|
||||
writes(b"abc",1usize,1usize,true,src,dst,heap);
|
||||
reads(b"abc",1usize,3usize,src,dst,heap);
|
||||
writes(b"abc",1usize,3usize,false,src,dst,heap);
|
||||
writes(b"abc",1usize,3usize,true,src,dst,heap);
|
||||
reads(b"abc",1usize,7usize,src,dst,heap);
|
||||
writes(b"abc",1usize,7usize,false,src,dst,heap);
|
||||
writes(b"abc",1usize,7usize,true,src,dst,heap);
|
||||
exact(b"abc",1usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"abc",1usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"abc",1usize,4usize,3usize,false,src,dst,heap);
|
||||
exact(b"abc",1usize,9usize,3usize,false,src,dst,heap);
|
||||
reads(b"abc",2usize,1usize,src,dst,heap);
|
||||
writes(b"abc",2usize,1usize,false,src,dst,heap);
|
||||
writes(b"abc",2usize,1usize,true,src,dst,heap);
|
||||
reads(b"abc",2usize,3usize,src,dst,heap);
|
||||
writes(b"abc",2usize,3usize,false,src,dst,heap);
|
||||
writes(b"abc",2usize,3usize,true,src,dst,heap);
|
||||
reads(b"abc",2usize,7usize,src,dst,heap);
|
||||
writes(b"abc",2usize,7usize,false,src,dst,heap);
|
||||
writes(b"abc",2usize,7usize,true,src,dst,heap);
|
||||
exact(b"abc",2usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"abc",2usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"abc",2usize,4usize,3usize,false,src,dst,heap);
|
||||
exact(b"abc",2usize,9usize,3usize,false,src,dst,heap);
|
||||
reads(b"abc",5usize,1usize,src,dst,heap);
|
||||
writes(b"abc",5usize,1usize,false,src,dst,heap);
|
||||
writes(b"abc",5usize,1usize,true,src,dst,heap);
|
||||
reads(b"abc",5usize,3usize,src,dst,heap);
|
||||
writes(b"abc",5usize,3usize,false,src,dst,heap);
|
||||
writes(b"abc",5usize,3usize,true,src,dst,heap);
|
||||
reads(b"abc",5usize,7usize,src,dst,heap);
|
||||
writes(b"abc",5usize,7usize,false,src,dst,heap);
|
||||
writes(b"abc",5usize,7usize,true,src,dst,heap);
|
||||
exact(b"abc",5usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"abc",5usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"abc",5usize,4usize,3usize,false,src,dst,heap);
|
||||
exact(b"abc",5usize,9usize,3usize,false,src,dst,heap);
|
||||
reads(b"abcdefgh",0usize,1usize,src,dst,heap);
|
||||
writes(b"abcdefgh",0usize,1usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",0usize,1usize,true,src,dst,heap);
|
||||
reads(b"abcdefgh",0usize,3usize,src,dst,heap);
|
||||
writes(b"abcdefgh",0usize,3usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",0usize,3usize,true,src,dst,heap);
|
||||
reads(b"abcdefgh",0usize,7usize,src,dst,heap);
|
||||
writes(b"abcdefgh",0usize,7usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",0usize,7usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",0usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",0usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",0usize,4usize,4usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",0usize,9usize,8usize,false,src,dst,heap);
|
||||
reads(b"abcdefgh",1usize,1usize,src,dst,heap);
|
||||
writes(b"abcdefgh",1usize,1usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",1usize,1usize,true,src,dst,heap);
|
||||
reads(b"abcdefgh",1usize,3usize,src,dst,heap);
|
||||
writes(b"abcdefgh",1usize,3usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",1usize,3usize,true,src,dst,heap);
|
||||
reads(b"abcdefgh",1usize,7usize,src,dst,heap);
|
||||
writes(b"abcdefgh",1usize,7usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",1usize,7usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",1usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",1usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",1usize,4usize,4usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",1usize,9usize,8usize,false,src,dst,heap);
|
||||
reads(b"abcdefgh",2usize,1usize,src,dst,heap);
|
||||
writes(b"abcdefgh",2usize,1usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",2usize,1usize,true,src,dst,heap);
|
||||
reads(b"abcdefgh",2usize,3usize,src,dst,heap);
|
||||
writes(b"abcdefgh",2usize,3usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",2usize,3usize,true,src,dst,heap);
|
||||
reads(b"abcdefgh",2usize,7usize,src,dst,heap);
|
||||
writes(b"abcdefgh",2usize,7usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",2usize,7usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",2usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",2usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",2usize,4usize,4usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",2usize,9usize,8usize,false,src,dst,heap);
|
||||
reads(b"abcdefgh",5usize,1usize,src,dst,heap);
|
||||
writes(b"abcdefgh",5usize,1usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",5usize,1usize,true,src,dst,heap);
|
||||
reads(b"abcdefgh",5usize,3usize,src,dst,heap);
|
||||
writes(b"abcdefgh",5usize,3usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",5usize,3usize,true,src,dst,heap);
|
||||
reads(b"abcdefgh",5usize,7usize,src,dst,heap);
|
||||
writes(b"abcdefgh",5usize,7usize,false,src,dst,heap);
|
||||
writes(b"abcdefgh",5usize,7usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",5usize,0usize,0usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",5usize,1usize,1usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",5usize,4usize,4usize,true,src,dst,heap);
|
||||
exact(b"abcdefgh",5usize,9usize,8usize,false,src,dst,heap);
|
||||
return 42;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// expect: reject E3002
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(file: file<read>,heap: cap<mem.alloc>) -> void effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,1usize,heap) { std.io.reader.NewResult::ok(value) => { trap; }, std.io.reader.NewResult::err(error) => { return; } }
|
||||
} fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,27 @@
|
||||
// expect: reject E4002
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn worker(q: ref<std.io.writer.Writer>) -> void effects [] {}
|
||||
fn bad(q: ref<std.io.writer.Writer>) -> void effects [task.spawn,task.wait] { scope s { let t = task.spawn(s,worker,q); task.join(move t); } }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,37 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:6
|
||||
// vfs: /in/data=abcdef
|
||||
// grant: dst=fs.write:/out
|
||||
// file: /out/data=abcdef
|
||||
// schedules: 12
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn work_read(value: std.io.reader.Reader) -> file<read> effects [fs.read] { var bytes: [u8;6] = [0u8;6]; { mut q = value; exact(std.io.reader.read_exact(q,mut bytes)); } assert(bytes[0] == 97u8); assert(bytes[5] == 102u8); return finished_reader(move value); }
|
||||
fn work_write(value: std.io.writer.Writer) -> file<write> effects [fs.write] { { mut q = value; written(std.io.writer.write(q,b"abcdef")); } return finished_writer(move value); }
|
||||
fn main(src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.read,fs.write,mem.alloc,task.spawn,task.wait] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap; let a = reader(move file,3usize,heap); let file = fs.create(dst,b"/out/data") or trap; let b = writer(move file,3usize,heap);
|
||||
scope s { let a = task.spawn(s,work_read,move a); let b = task.spawn(s,work_write,move b); fs.close(task.join(move a)) or trap; fs.close(task.join(move b)) or trap; } return 42;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap; let value = reader(move file,3usize,heap); var bytes: [u8;8] = [255u8;8];
|
||||
{ mut q = value; exact(std.io.reader.read_exact(q,mut bytes[1usize..5usize]));
|
||||
match std.io.reader.read_exact(q,mut bytes[5usize..8usize]) { std.io.ReadExactResult::eof(count) => { assert(count == 2usize); }, std.io.ReadExactResult::ok => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
assert(bytes[0] == 255u8); assert(bytes[1] == 97u8); assert(bytes[2] == 98u8); assert(bytes[3] == 99u8); assert(bytes[4] == 100u8); assert(bytes[5] == 101u8); assert(bytes[6] == 102u8); assert(bytes[7] == 255u8);
|
||||
fs.close(finished_reader(move value)) or trap; let allocation = own.new([0u8;3],heap) or trap; own.free(move allocation); return 42;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E2101
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(q: mut<std.io.reader.Reader>) -> void effects [fs.read] { mut storage = q.storage; mut bytes = buffer.view(storage); std.io.reader.read(q,bytes); }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,34 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap; let value = reader(move file,3usize,heap); var byte: [u8;1] = [0u8;1];
|
||||
{ mut q = value; match std.io.reader.read(q,mut byte) { std.io.reader.ReadResult::ok(n) => { assert(n == 1usize); }, std.io.reader.ReadResult::err(error) => { trap; } } }
|
||||
let (file,unread) = struct.unpack(std.io.reader.discard(move value)); assert(unread == 2usize); exact(std.io.read_exact_file(mut file,mut byte)); assert(byte[0] == 100u8); fs.close(move file) or trap;
|
||||
let allocation = own.new([0u8;3],heap) or trap; own.free(move allocation); return 42;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap;
|
||||
match std.io.reader.new(move file,4usize,heap) {
|
||||
std.io.reader.NewResult::ok(value) => { trap; }, std.io.reader.NewResult::err(error) => {
|
||||
let (file,error) = struct.unpack(move error); var bytes: [u8;3] = [0u8;3]; exact(std.io.read_exact_file(mut file,mut bytes)); assert(bytes[0] == 97u8); fs.close(move file) or trap;
|
||||
}
|
||||
}
|
||||
let allocation = own.new([0u8;3],heap) or trap; own.free(move allocation); return 42;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:0
|
||||
// vfs: /in/data=abcdef
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap; let value = reader(move file,0usize,heap); var bytes: [u8;7] = [0u8;7];
|
||||
{ mut q = value; match std.io.reader.read_exact(q,mut bytes) { std.io.ReadExactResult::eof(n) => { assert(n == 6usize); }, std.io.ReadExactResult::ok => { trap; }, std.io.ReadExactResult::err(error) => { trap; } } }
|
||||
assert(bytes[0] == 97u8); assert(bytes[5] == 102u8); fs.close(finished_reader(move value)) or trap; return 42;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
// grant: dst=fs.write:/out
|
||||
// file: /out/data=
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.read,fs.write,mem.alloc] {
|
||||
let narrow = cap.narrow.budget(heap,3usize) or trap; let file = fs.open_read(src,b"/in/data") or trap; let value = reader(move file,3usize,narrow); fs.close(finished_reader(move value)) or trap;
|
||||
let file = fs.create(dst,b"/out/data") or trap; let value = writer(move file,3usize,narrow); fs.close(finished_writer(move value)) or trap;
|
||||
let allocation = own.new([0u8;3],narrow) or trap; own.free(move allocation); return 42;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/in
|
||||
// grant: heap=mem.alloc:3
|
||||
// vfs: /in/data=abcdef
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc] {
|
||||
let file = fs.open_read(src,b"/in/data") or trap; let value = reader(move file,3usize,heap); var byte: [u8;1] = [0u8;1];
|
||||
{ mut q = value; match std.io.reader.read(q,mut byte) { std.io.reader.ReadResult::ok(n) => { assert(n == 1usize); }, std.io.reader.ReadResult::err(error) => { trap; } } }
|
||||
assert(byte[0] == 97u8);
|
||||
match std.io.reader.finish(move value) {
|
||||
std.io.reader.FinishResult::ok(file) => { trap; },
|
||||
std.io.reader.FinishResult::unread(value) => {
|
||||
var tail: [u8;5] = [0u8;5]; { mut q = value; exact(std.io.reader.read_exact(q,mut tail)); }
|
||||
assert(tail[0] == 98u8); assert(tail[4] == 102u8); fs.close(finished_reader(move value)) or trap;
|
||||
}
|
||||
}
|
||||
let allocation = own.new([0u8;3],heap) or trap; own.free(move allocation); return 42;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// expect: reject E3002
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(value: std.io.reader.Reader) -> void effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { trap; }, std.io.reader.FinishResult::unread(value) => { return; } }
|
||||
} fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,33 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: dst=fs.write:/out
|
||||
// grant: heap=mem.alloc:3
|
||||
// file: /out/data=abcdef
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.write,mem.alloc] {
|
||||
let file = fs.create(dst,b"/out/data") or trap; let value = writer(move file,3usize,heap);
|
||||
{ mut q = value; written(std.io.writer.write(q,b"ab")); written(std.io.writer.write(q,b"cd")); written(std.io.writer.write(q,b"ef")); }
|
||||
fs.close(finished_writer(move value)) or trap; let allocation = own.new([0u8;3],heap) or trap; own.free(move allocation); return 42;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E2101
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(q: mut<std.io.writer.Writer>) -> void effects [fs.write] { ref storage = q.storage; ref bytes = buffer.view(storage); std.io.writer.write(q,bytes); }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,33 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: dst=fs.write:/out
|
||||
// grant: heap=mem.alloc:3
|
||||
// file: /out/data=z
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.write,mem.alloc] {
|
||||
let file = fs.create(dst,b"/out/data") or trap; let value = writer(move file,3usize,heap); { mut q = value; written(std.io.writer.write(q,b"abc")); }
|
||||
let (file,pending,failed) = struct.unpack(std.io.writer.discard(move value)); assert(pending == 3usize); assert(not std.option.is_some::<io_error>(failed)); fs.write_all(file,b"z") or trap; fs.close(move file) or trap;
|
||||
let allocation = own.new([0u8;3],heap) or trap; own.free(move allocation); return 42;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: dst=fs.write:/out
|
||||
// grant: heap=mem.alloc:3
|
||||
// file: /out/data=abc
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.write,mem.alloc] {
|
||||
let file = fs.create(dst,b"/out/data") or trap;
|
||||
match std.io.writer.new(move file,4usize,heap) {
|
||||
std.io.writer.NewResult::ok(value) => { trap; }, std.io.writer.NewResult::err(error) => { let (file,error) = struct.unpack(move error); fs.write_all(file,b"abc") or trap; fs.close(move file) or trap; }
|
||||
}
|
||||
let allocation = own.new([0u8;3],heap) or trap; own.free(move allocation); return 42;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E3002
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(file: file<write>,heap: cap<mem.alloc>) -> void effects [mem.alloc] { match std.io.writer.new(move file,3usize,heap) { std.io.writer.NewResult::ok(q) => { trap; }, std.io.writer.NewResult::err(error) => { return; } } }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,32 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: dst=fs.write:/out
|
||||
// grant: heap=mem.alloc:0
|
||||
// file: /out/data=abc
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn main(dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.write,mem.alloc] {
|
||||
let file = fs.create(dst,b"/out/data") or trap; let value = writer(move file,0usize,heap); { mut q = value; written(std.io.writer.write(q,b"abc")); match std.io.writer.flush(q) { std.io.writer.FlushResult::ok => {}, std.io.writer.FlushResult::err(error) => { trap; } } }
|
||||
fs.close(finished_writer(move value)) or trap; return 42;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(file: file<read>,heap: cap<fs.read>) -> void effects [mem.alloc] { std.io.reader.new(move file,3usize,heap); trap; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,26 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
module test.buffered
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
fn reader(file: file<read>,capacity: usize,heap: cap<mem.alloc>) -> std.io.reader.Reader effects [mem.alloc] {
|
||||
match std.io.reader.new(move file,capacity,heap) { std.io.reader.NewResult::ok(value) => { return move value; }, std.io.reader.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn writer(file: file<write>,capacity: usize,heap: cap<mem.alloc>) -> std.io.writer.Writer effects [mem.alloc] {
|
||||
match std.io.writer.new(move file,capacity,heap) { std.io.writer.NewResult::ok(value) => { return move value; }, std.io.writer.NewResult::err(error) => { trap; } }
|
||||
}
|
||||
fn exact(result: std.io.ReadExactResult) -> void effects [] {
|
||||
match result { std.io.ReadExactResult::ok => {}, std.io.ReadExactResult::eof(count) => { trap; }, std.io.ReadExactResult::err(error) => { trap; } }
|
||||
}
|
||||
fn written(result: std.io.writer.WriteResult) -> void effects [] { match result { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } }
|
||||
fn finished_reader(value: std.io.reader.Reader) -> file<read> effects [] {
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { return move file; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
fn finished_writer(value: std.io.writer.Writer) -> file<write> effects [fs.write] {
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { return move file; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
fn bad(file: file<append>,heap: cap<mem.alloc>) -> void effects [mem.alloc] { std.io.writer.new(move file,3usize,heap); trap; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,188 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Owning buffered adapters: CLI/AIR/packages, hosted failure and no retry."""
|
||||
import base64
|
||||
import json
|
||||
from pathlib import Path
|
||||
import resource
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
REPO = Path(__file__).resolve().parents[2]
|
||||
AJI = REPO / "zig-out/bin/aji"
|
||||
CASES = REPO / "tests/conformance/source/buffered_io"
|
||||
checks = 0
|
||||
|
||||
|
||||
def cli(*args, code=0, **kwargs):
|
||||
global checks
|
||||
run = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True, **kwargs)
|
||||
detail = None
|
||||
if run.returncode != code and "--result-file" in args:
|
||||
result = Path(args[args.index("--result-file") + 1])
|
||||
if result.exists():
|
||||
detail = result.read_text()
|
||||
assert run.returncode == code, (args, run.returncode, run.stdout, run.stderr, detail)
|
||||
checks += 1
|
||||
return run.stdout
|
||||
|
||||
|
||||
def returned(path, *grants, extra=()):
|
||||
args = [item for grant in grants for item in ("--grant", grant)]
|
||||
record = json.loads(cli("run", "--fuel", "2000000", *args, *extra, path))
|
||||
assert record["status"] == "returned", record
|
||||
return record
|
||||
|
||||
|
||||
example = REPO / "examples/std/buffered_io.aji"
|
||||
grants = ("src=fs.read:/in", "dst=fs.write:/out", "heap=mem.alloc:6")
|
||||
record = returned(example, *grants, extra=("--vfs", "/in/data=abcdefgh"))
|
||||
assert record["value"] == 8 and base64.b64decode(record["files"]["/out/data"]["data"]) == b"abcdefgh", record
|
||||
cli("check", example)
|
||||
report = json.loads(cli("report", example))
|
||||
assert report["unsafe"]["operations"] == 0 and report["unsafe"]["trusted_functions"] == [], report
|
||||
canonical = cli("lower", example)
|
||||
assert canonical == cli("lower", example)
|
||||
for path in sorted(CASES.glob("*.aji")):
|
||||
if "// expect: run\n" not in path.read_text():
|
||||
continue
|
||||
args = []
|
||||
for line in path.read_text().splitlines():
|
||||
if line.startswith("// grant: "):
|
||||
args.extend(("--grant", line.removeprefix("// grant: ")))
|
||||
if line.startswith("// vfs: "):
|
||||
args.extend(("--vfs", line.removeprefix("// vfs: ")))
|
||||
assert returned(path, extra=args)["value"] == 42
|
||||
for name, expected in (("missing_read_effect", "E3101"), ("missing_write_effect", "E3101"),
|
||||
("missing_alloc_effect", "E3101"), ("wrong_kind", "E7005"), ("wrong_mode", "E7005"),
|
||||
("missing_move", "E7003"), ("discard_leak", "E3002"), ("finish_error_leak", "E3002"),
|
||||
("reader_alias", "E2101"), ("writer_alias", "E2101"), ("loan", "E4101"), ("not_share", "E4002")):
|
||||
commands = ("check", "lower", "report", "run", "explore") if expected in ("E7003", "E7005") else ("check", "report", "run", "explore")
|
||||
for command in commands:
|
||||
path = CASES / (name + ".aji")
|
||||
errors = [json.loads(line) for line in cli(command, path, code=1).splitlines()]
|
||||
assert errors and {error["code"] for error in errors} == {expected}, errors
|
||||
assert all(Path(error["file"]).resolve() == path.resolve() for error in errors), errors
|
||||
|
||||
CHECK_ERROR = '''fn error_is_other(error: io_error) -> void effects [] {
|
||||
match error { io_error::other => {}, _ => { trap; } }
|
||||
}
|
||||
fn flush_failed(result: std.io.writer.FlushResult) -> void effects [] {
|
||||
match result { std.io.writer.FlushResult::err(error) => { error_is_other(error); }, std.io.writer.FlushResult::ok => { trap; } }
|
||||
}
|
||||
fn write_failed(result: std.io.writer.WriteResult,accepted: usize) -> void effects [] {
|
||||
match result { std.io.writer.WriteResult::err(error) => { assert(error.accepted == accepted); error_is_other(error.error); }, std.io.writer.WriteResult::ok => { trap; } }
|
||||
}
|
||||
fn pending(value: ref<std.io.writer.Writer>,expected: usize) -> void effects [] {
|
||||
assert(value.used == expected);
|
||||
if expected != 0usize { ref storage = value.storage; ref bytes = buffer.view(storage); assert(bytes[0] == 99u8); assert(bytes[1] == 100u8); }
|
||||
}
|
||||
'''
|
||||
# Enum matches are exhaustive (no enum wildcard); construct the remaining arms.
|
||||
TAGS = ("not_found", "permission_denied", "already_exists", "invalid_input", "no_space", "broken_pipe",
|
||||
"connection_refused", "connection_reset", "address_in_use", "address_unavailable", "timed_out")
|
||||
CHECK_ERROR = CHECK_ERROR.replace('_ => { trap; }', ', '.join(f'io_error::{tag} => {{ trap; }}' for tag in TAGS))
|
||||
with tempfile.TemporaryDirectory(prefix="aji-buffered-io-") as directory:
|
||||
root = Path(directory)
|
||||
air = root / "lowered.air"
|
||||
air.write_text(canonical)
|
||||
cli("check", air)
|
||||
assert returned(air, *grants, extra=("--vfs", "/in/data=abcdefgh")) == record
|
||||
entry, manifest = root / "main.aji", root / "aji.toml"
|
||||
entry.write_text(example.read_text())
|
||||
manifest.write_text('[package]\nname = "buffered"\nversion = "1"\nentry = "main.aji"\n[capabilities]\nsrc = "fs.read:/in"\ndst = "fs.write:/out"\nheap = "mem.alloc:6"\n')
|
||||
package = json.loads(cli("run", "--fuel", "2000000", "--vfs", "/in/data=abcdefgh", "--manifest", manifest))
|
||||
assert package == record, package
|
||||
assert cli("lower", "--manifest", manifest) == canonical
|
||||
entry.write_text(example.read_text().replace('import std.io.reader;\nimport std.io.writer;', 'import std.io.writer;\nimport std.io.reader;'))
|
||||
assert cli("lower", "--manifest", manifest) == canonical
|
||||
source, target, result = root / "input", root / "output", root / "result.json"
|
||||
source.write_text("abcdefgh")
|
||||
entry.write_text(example.read_text().replace('b"/in/data"', f'b"{source}"').replace('b"/out/data"', f'b"{target}"'))
|
||||
stdout = cli("run", "--profile", "hosted", "--fuel", "2000000", "--grant", f"src=fs.read:{root}", "--grant", f"dst=fs.write:{root}", "--grant", "heap=mem.alloc:6", "--result-file", result, entry)
|
||||
assert stdout == "" and json.loads(result.read_text())["value"] == 8 and target.read_bytes() == b"abcdefgh"
|
||||
# Linux /proc/self/mem opens normally but offset zero cannot be read.
|
||||
# No memory contents are read; the hosted primitive returns EIO / other.
|
||||
if sys.platform.startswith("linux"):
|
||||
entry.write_text('''module app.read_error
|
||||
import std.io;
|
||||
import std.io.reader;
|
||||
''' + CHECK_ERROR.split("fn flush_failed")[0] + '''fn main(src: cap<fs.read>,heap: cap<mem.alloc>) -> u64 effects [fs.read,mem.alloc] {
|
||||
let file = fs.open_read(src,b"/proc/self/mem") or trap;
|
||||
match std.io.reader.new(move file,3usize,heap) {
|
||||
std.io.reader.NewResult::err(error) => { trap; }, std.io.reader.NewResult::ok(value) => {
|
||||
var bytes: [u8;6] = [99u8;6];
|
||||
{ mut q = value; match std.io.reader.read_exact(q,mut bytes[1usize..5usize]) {
|
||||
std.io.ReadExactResult::err(error) => { assert(error.completed == 0usize); error_is_other(error.error); }, std.io.ReadExactResult::ok => { trap; }, std.io.ReadExactResult::eof(n) => { trap; }
|
||||
}
|
||||
assert(q.begin == 0usize); assert(q.end == 0usize);
|
||||
match std.io.reader.read(q,mut bytes[0usize..0usize]) { std.io.reader.ReadResult::ok(n) => { assert(n == 0usize); }, std.io.reader.ReadResult::err(error) => { trap; } }
|
||||
match std.io.reader.read(q,mut bytes[1usize..5usize]) { std.io.reader.ReadResult::err(error) => { error_is_other(error); }, std.io.reader.ReadResult::ok(n) => { trap; } }
|
||||
}
|
||||
assert(bytes[0] == 99u8); assert(bytes[5] == 99u8);
|
||||
let (file,unread) = struct.unpack(std.io.reader.discard(move value)); assert(unread == 0usize); fs.close(move file) or trap;
|
||||
}
|
||||
}
|
||||
let allocation = own.new([0u8;3],heap) or trap; own.free(move allocation); return 42;
|
||||
}
|
||||
''')
|
||||
cli("run", "--profile", "hosted", "--fuel", "2000000", "--grant", "src=fs.read:/proc/self", "--grant", "heap=mem.alloc:3", "--result-file", result, entry)
|
||||
assert json.loads(result.read_text())["value"] == 42
|
||||
|
||||
def partial_write_limit():
|
||||
signal.signal(signal.SIGXFSZ, signal.SIG_IGN)
|
||||
resource.setrlimit(resource.RLIMIT_FSIZE, (3, 1048576))
|
||||
|
||||
for mode in ("room", "prior", "flush", "direct"):
|
||||
capacity = 0 if mode == "direct" else 2
|
||||
initial = {
|
||||
"direct": 'write_failed(std.io.writer.write(q,b"abcdef"),0usize);',
|
||||
"room": 'write_failed(std.io.writer.write(q,b"abcdef"),4usize);',
|
||||
"prior": 'match std.io.writer.write(q,b"ab") { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } write_failed(std.io.writer.write(q,b"cdefgh"),2usize);',
|
||||
"flush": 'match std.io.writer.write(q,b"abcd") { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } } flush_failed(std.io.writer.flush(q));',
|
||||
}[mode]
|
||||
entry.write_text('''module app.partial_write
|
||||
import std.io;
|
||||
import std.io.writer;
|
||||
import std.option;
|
||||
''' + CHECK_ERROR + f'''fn main(dst: cap<fs.write>,heap: cap<mem.alloc>,input: cap<io.read>,print: cap<io.write>) -> u64 effects [fs.write,mem.alloc,io.read,io.write] {{
|
||||
let file = fs.create(dst,b"{target}") or trap;
|
||||
match std.io.writer.new(move file,{capacity}usize,heap) {{
|
||||
std.io.writer.NewResult::err(error) => {{ trap; }}, std.io.writer.NewResult::ok(value) => {{
|
||||
{{ mut q = value; {initial} pending(ref q,{capacity}usize);
|
||||
match q.failed {{ std.option.Option::<io_error>::some(error) => {{ error_is_other(error); }}, std.option.Option::<io_error>::none => {{ trap; }} }}
|
||||
}}
|
||||
io.stdout_write(print,b"E") or trap;
|
||||
var ack: [u8;1] = [0u8;1]; match std.io.read_exact_stdin(input,mut ack) {{ std.io.ReadExactResult::ok => {{}}, std.io.ReadExactResult::eof(n) => {{ trap; }}, std.io.ReadExactResult::err(error) => {{ trap; }} }} assert(ack[0] == 89u8);
|
||||
{{ mut q = value; flush_failed(std.io.writer.flush(q)); write_failed(std.io.writer.write(q,b"z"),0usize); write_failed(std.io.writer.write(q,ref ack[0usize..0usize]),0usize); pending(ref q,{capacity}usize); }}
|
||||
match std.io.writer.finish(move value) {{
|
||||
std.io.writer.FinishResult::ok(file) => {{ trap; }}, std.io.writer.FinishResult::err(error) => {{
|
||||
let (writer,error) = struct.unpack(move error); error_is_other(error);
|
||||
let (file,pending,failed) = struct.unpack(std.io.writer.discard(move writer)); assert(pending == {capacity}usize);
|
||||
match failed {{ std.option.Option::<io_error>::some(error) => {{ error_is_other(error); }}, std.option.Option::<io_error>::none => {{ trap; }} }} fs.close(move file) or trap;
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
let allocation = own.new([0u8;2],heap) or trap; own.free(move allocation); io.stdout_write(print,b"42") or trap; return 42;
|
||||
}}
|
||||
''')
|
||||
cli("check", entry)
|
||||
command = [str(AJI), "run", "--profile", "hosted", "--fuel", "2000000", "--grant", f"dst=fs.write:{root}", "--grant", "heap=mem.alloc:2", "--grant", "input=io.read", "--grant", "print=io.write", str(entry)]
|
||||
with subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=partial_write_limit) as process:
|
||||
marker = process.stdout.read(1)
|
||||
if marker != b"E":
|
||||
output, error = process.communicate()
|
||||
raise AssertionError((mode, marker, process.returncode, output, error))
|
||||
assert target.read_bytes() == b"abc", (mode, target.read_bytes())
|
||||
# On Linux raise the child's quota before subsequent calls. A retry
|
||||
# would now succeed and append duplicated buffered bytes to the file.
|
||||
if hasattr(resource, "prlimit"):
|
||||
resource.prlimit(process.pid, resource.RLIMIT_FSIZE, (1048576, 1048576))
|
||||
output, error = process.communicate(b"Y")
|
||||
assert process.returncode == 0 and output == b"42", (mode, process.returncode, output, error)
|
||||
assert target.read_bytes() == b"abc", (mode, target.read_bytes())
|
||||
checks += 1
|
||||
|
||||
print(f"buffered I/O integration passed ({checks} checks, partial room/flush/direct writes; Linux read error and raised-quota no retry)")
|
||||
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Independent byte oracle for bounded adapters; --write regenerates fixture."""
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1] / "tests/conformance/source/buffered_io"
|
||||
HELPERS = '''import std.io;
|
||||
import std.io.reader;
|
||||
import std.io.writer;
|
||||
import std.bytes;
|
||||
fn prepare(contents: slice<u8>,dst: cap<fs.write>) -> void effects [fs.write] {
|
||||
let file = fs.create(dst,b"/case/input") or trap; fs.write_all(file,contents) or trap; fs.close(move file) or trap;
|
||||
}
|
||||
fn reads(contents: slice<u8>,capacity: usize,chunk: usize,src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> void effects [fs.read,fs.write,mem.alloc] {
|
||||
prepare(contents,dst); let file = fs.open_read(src,b"/case/input") or trap;
|
||||
match std.io.reader.new(move file,capacity,heap) {
|
||||
std.io.reader.NewResult::err(error) => { trap; }, std.io.reader.NewResult::ok(value) => {
|
||||
var bytes: [u8;9] = [255u8;9]; var offset: usize = 0;
|
||||
{ mut q = value; loop {
|
||||
match std.io.reader.read(q,mut bytes[1usize..chunk+1usize]) {
|
||||
std.io.reader.ReadResult::err(error) => { trap; }, std.io.reader.ReadResult::ok(count) => {
|
||||
assert(count <= chunk); assert(offset + count <= contents.len);
|
||||
assert(std.bytes.equal(ref bytes[1usize..count+1usize],ref contents[offset..offset+count]));
|
||||
assert(bytes[0] == 255u8); for i in chunk+1usize..9usize { assert(bytes[i] == 255u8); }
|
||||
offset = offset + count; if count == 0usize { break; }
|
||||
}
|
||||
}
|
||||
} }
|
||||
assert(offset == contents.len);
|
||||
match std.io.reader.finish(move value) { std.io.reader.FinishResult::ok(file) => { fs.close(move file) or trap; }, std.io.reader.FinishResult::unread(value) => { trap; } }
|
||||
}
|
||||
}
|
||||
}
|
||||
fn exact(contents: slice<u8>,capacity: usize,requested: usize,prefix: usize,full: bool,src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> void effects [fs.read,fs.write,mem.alloc] {
|
||||
prepare(contents,dst); let file = fs.open_read(src,b"/case/input") or trap;
|
||||
match std.io.reader.new(move file,capacity,heap) {
|
||||
std.io.reader.NewResult::err(error) => { trap; }, std.io.reader.NewResult::ok(value) => {
|
||||
var bytes: [u8;12] = [255u8;12];
|
||||
{ mut q = value; match std.io.reader.read_exact(q,mut bytes[1usize..requested+1usize]) {
|
||||
std.io.ReadExactResult::ok => { assert(full); }, std.io.ReadExactResult::eof(n) => { assert(not full); assert(n == prefix); }, std.io.ReadExactResult::err(error) => { trap; }
|
||||
} }
|
||||
assert(std.bytes.equal(ref bytes[1usize..prefix+1usize],ref contents[0usize..prefix]));
|
||||
assert(bytes[0] == 255u8); for i in prefix+1usize..12usize { assert(bytes[i] == 255u8); }
|
||||
let (file,unread) = struct.unpack(std.io.reader.discard(move value)); fs.close(move file) or trap;
|
||||
}
|
||||
}
|
||||
}
|
||||
fn writes(contents: slice<u8>,capacity: usize,chunk: usize,flush: bool,src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> void effects [fs.read,fs.write,mem.alloc] {
|
||||
let file = fs.create(dst,b"/case/output") or trap;
|
||||
match std.io.writer.new(move file,capacity,heap) {
|
||||
std.io.writer.NewResult::err(error) => { trap; }, std.io.writer.NewResult::ok(value) => {
|
||||
var offset: usize = 0;
|
||||
{ mut q = value; match std.io.writer.write(q,ref contents[0usize..0usize]) { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } }
|
||||
while offset < contents.len {
|
||||
let left = contents.len - offset; let count = if left < chunk { left } else { chunk };
|
||||
match std.io.writer.write(q,ref contents[offset..offset+count]) { std.io.writer.WriteResult::ok => {}, std.io.writer.WriteResult::err(error) => { trap; } }
|
||||
offset = offset + count;
|
||||
if flush { match std.io.writer.flush(q) { std.io.writer.FlushResult::ok => {}, std.io.writer.FlushResult::err(error) => { trap; } } }
|
||||
}
|
||||
}
|
||||
match std.io.writer.finish(move value) { std.io.writer.FinishResult::ok(file) => { fs.close(move file) or trap; }, std.io.writer.FinishResult::err(error) => { trap; } }
|
||||
}
|
||||
}
|
||||
let file = fs.open_read(src,b"/case/output") or trap; var bytes: [u8;12] = [255u8;12];
|
||||
match std.io.read_to_end_file(mut file,mut bytes[1usize..11usize]) { std.io.ReadToEndResult::eof(n) => { assert(n == contents.len); assert(std.bytes.equal(ref bytes[1usize..n+1usize],contents)); }, std.io.ReadToEndResult::full(n) => { trap; }, std.io.ReadToEndResult::err(error) => { trap; } }
|
||||
assert(bytes[0] == 255u8); assert(bytes[11] == 255u8); fs.close(move file) or trap;
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
def literal(text):
|
||||
return 'b"' + text + '"' if text else "ref blank[0usize..0usize]"
|
||||
|
||||
|
||||
calls = []
|
||||
for length in (0, 1, 3, 8):
|
||||
text = "abcdefgh"[:length]
|
||||
for capacity in (0, 1, 2, 5):
|
||||
for chunk in (1, 3, 7):
|
||||
calls.append(f"reads({literal(text)},{capacity}usize,{chunk}usize,src,dst,heap);")
|
||||
for flush in (False, True):
|
||||
calls.append(f"writes({literal(text)},{capacity}usize,{chunk}usize,{str(flush).lower()},src,dst,heap);")
|
||||
for requested in (0, 1, 4, 9):
|
||||
calls.append(f"exact({literal(text)},{capacity}usize,{requested}usize,{min(length,requested)}usize,{str(requested <= length).lower()},src,dst,heap);")
|
||||
expected = '''// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.18
|
||||
// grant: src=fs.read:/case
|
||||
// grant: dst=fs.write:/case
|
||||
// grant: heap=mem.alloc:5
|
||||
// file: /case/input=abcdefgh
|
||||
// file: /case/output=abcdefgh
|
||||
// fuel: 2000000
|
||||
module test.buffered_model
|
||||
''' + HELPERS + "fn main(src: cap<fs.read>,dst: cap<fs.write>,heap: cap<mem.alloc>) -> u64 effects [fs.read,fs.write,mem.alloc] {\nlet blank: [u8;1] = [0u8;1];\n" + "\n".join(calls) + "\nreturn 42;\n}\n"
|
||||
path = ROOT / "model.aji"
|
||||
if "--write" in sys.argv:
|
||||
path.write_text(expected)
|
||||
assert path.read_text() == expected, "Buffered I/O fixture differs from independent byte oracle"
|
||||
print("buffered I/O oracle passed (48 read streams, 64 exact prefixes, 96 segmented writes; bytes, guards, EOF and zero capacity)")
|
||||
@@ -27,6 +27,10 @@ CHECKLIST = [
|
||||
("heap", "explicit order and bounded owning push", r"std\.heap\.push::<", True),
|
||||
("heap", "Copy peek and moving pop", r"std\.heap\.(peek|pop)::<", True),
|
||||
("heap", "explicit heap growth and drain", r"std\.heap\.(reserve|free)::<", True),
|
||||
("buffered io", "owned reader construction and explicit termination", r"std\.io\.reader\.(new|finish|discard)\(", True),
|
||||
("buffered io", "borrowed cached reads and exact prefixes", r"std\.io\.reader\.(read|read_exact)\(", True),
|
||||
("buffered io", "owned writer construction and explicit termination", r"std\.io\.writer\.(new|finish|discard)\(", True),
|
||||
("buffered io", "borrowed writes and explicit flush", r"std\.io\.writer\.(write|flush)\(", True),
|
||||
("io", "exact reads with explicit effects and borrowed destinations", r"std\.io\.read_exact_(stdin|file|stream)\(", True),
|
||||
("io", "bounded file read/copy and explicit progress", r"std\.io\.(read_to_end_file|copy_file|copy_file_append)\(", True),
|
||||
("static functions", "explicit callable contract", r"[A-Z]\w*: fn\(", True),
|
||||
|
||||
Reference in new issue
Block a user