io interface (first pass)
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
io :: import "@std/io"
|
||||
|
||||
_read_ok func(_ ?*mut anyopaque, _ io.ReadStream, buffer []mut u8) usize ! io.ReadError {
|
||||
if buffer.len == 0 {
|
||||
return 0
|
||||
}
|
||||
buffer[0] = 'o'
|
||||
if buffer.len == 1 {
|
||||
return 1
|
||||
}
|
||||
buffer[1] = 'k'
|
||||
return 2
|
||||
}
|
||||
|
||||
_read_too_much func(_ ?*mut anyopaque, _ io.ReadStream, buffer []mut u8) usize ! io.ReadError {
|
||||
return buffer.len + 1
|
||||
}
|
||||
|
||||
_read_eof func(_ ?*mut anyopaque, _ io.ReadStream, _ []mut u8) usize ! io.ReadError {
|
||||
return 0
|
||||
}
|
||||
|
||||
_write_short func(_ ?*mut anyopaque, _ io.WriteStream, bytes []u8) usize ! io.WriteError {
|
||||
if bytes.len > 2 {
|
||||
return 2
|
||||
}
|
||||
return bytes.len
|
||||
}
|
||||
|
||||
_write_none func(_ ?*mut anyopaque, _ io.WriteStream, _ []u8) usize ! io.WriteError {
|
||||
return 0
|
||||
}
|
||||
|
||||
_write_too_much func(_ ?*mut anyopaque, _ io.WriteStream, bytes []u8) usize ! io.WriteError {
|
||||
return bytes.len + 1
|
||||
}
|
||||
|
||||
_ok_vtable io.IoVTable :: io.IoVTable {
|
||||
read = _read_ok,
|
||||
write = _write_short,
|
||||
}
|
||||
|
||||
_read_bad_vtable io.IoVTable :: io.IoVTable {
|
||||
read = _read_too_much,
|
||||
write = _write_short,
|
||||
}
|
||||
|
||||
_eof_vtable io.IoVTable :: io.IoVTable {
|
||||
read = _read_eof,
|
||||
write = _write_short,
|
||||
}
|
||||
|
||||
_write_none_vtable io.IoVTable :: io.IoVTable {
|
||||
read = _read_ok,
|
||||
write = _write_none,
|
||||
}
|
||||
|
||||
_write_bad_vtable io.IoVTable :: io.IoVTable {
|
||||
read = _read_ok,
|
||||
write = _write_too_much,
|
||||
}
|
||||
|
||||
reader_for func(vtable @io.IoVTable) io.Reader {
|
||||
return io.Reader {
|
||||
impl = io.Io {context = none, vtable = vtable},
|
||||
stream = .stdin,
|
||||
}
|
||||
}
|
||||
|
||||
writer_for func(vtable @io.IoVTable) io.Writer {
|
||||
return io.Writer {
|
||||
impl = io.Io {context = none, vtable = vtable},
|
||||
stream = .stdout,
|
||||
}
|
||||
}
|
||||
|
||||
rejects_bad_read func() bool {
|
||||
buffer [1]mut u8 = [0]
|
||||
_ = io.read(reader_for(&_read_bad_vtable), buffer[..]) catch |err| {
|
||||
return err == .read_failed
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
rejects_no_progress func() bool {
|
||||
io.write_all(writer_for(&_write_none_vtable), "x") catch |err| {
|
||||
return err == .no_progress
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
rejects_bad_write func() bool {
|
||||
_ = io.write(writer_for(&_write_bad_vtable), "x") catch |err| {
|
||||
return err == .write_failed
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
main func(system io.Io) i32 {
|
||||
buffer [2]mut u8 = [0, 0]
|
||||
count usize :: io.read(reader_for(&_ok_vtable), buffer[..]) catch 0
|
||||
if count != 2 or buffer[0] != 'o' or buffer[1] != 'k' {
|
||||
return 1
|
||||
}
|
||||
eof usize :: io.read(reader_for(&_eof_vtable), buffer[..]) catch 1
|
||||
empty_read usize :: io.read(reader_for(&_read_bad_vtable), buffer[0..0]) catch 1
|
||||
empty_write usize :: io.write(writer_for(&_write_bad_vtable), "") catch 1
|
||||
if eof != 0 or empty_read != 0 or empty_write != 0 {
|
||||
return 5
|
||||
}
|
||||
io.write_all(writer_for(&_ok_vtable), "partial") catch |_| {
|
||||
return 2
|
||||
}
|
||||
if !rejects_bad_read() or !rejects_no_progress() or !rejects_bad_write() {
|
||||
return 3
|
||||
}
|
||||
io.write_all(io.Writer {
|
||||
impl = system,
|
||||
stream = .stdout,
|
||||
}, "io-ok\n") catch |_| {
|
||||
return 4
|
||||
}
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user