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
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user