Update coral library to use new reader / writer system

This commit is contained in:
kayomn 2023-02-20 00:34:46 +00:00
parent 9ac61b614f
commit 924780d92c
1 changed files with 21 additions and 19 deletions

View File

@ -450,9 +450,13 @@ export namespace coral {
unavailable, unavailable,
}; };
using readable = callable<expected<usize, io_error>(slice<u8> const &)>; struct reader {
virtual expected<usize, io_error> read(slice<u8> const & buffer) = 0;
};
using writable = callable<expected<usize, io_error>(slice<u8 const> const &)>; struct writer {
virtual expected<usize, io_error> write(slice<u8 const> const & buffer) = 0;
};
} }
// Input/output operations. // Input/output operations.
@ -533,30 +537,28 @@ export namespace coral {
* *Note*: if `buffer` has a length of `0`, no data will be streamed as there is nowhere to * *Note*: if `buffer` has a length of `0`, no data will be streamed as there is nowhere to
* temporarily place data during streaming. * temporarily place data during streaming.
*/ */
expected<usize, io_error> stream(writable const & output, expected<usize, io_error> stream(writer & output, reader & input, slice<u8> const & buffer) {
readable const & input, slice<u8> const & buffer) { usize total_bytes_written = 0;
expected bytes_read = input.read(buffer);
usize written = 0; if (!bytes_read.is_ok()) return bytes_read.error();
expected maybe_read = input(buffer);
if (!maybe_read.is_ok()) return maybe_read.error(); usize read = bytes_read.value();
usize read = maybe_read.value();
while (read != 0) { while (read != 0) {
expected const maybe_written = output(buffer.sliced(0, read)); expected const bytes_written = output.write(buffer.sliced(0, read));
if (!maybe_written.is_ok()) return maybe_read.error(); if (!bytes_written.is_ok()) return bytes_read.error();
written += maybe_written.value(); total_bytes_written += bytes_written.value();
maybe_read = input(buffer); bytes_read = input.read(buffer);
if (!maybe_read.is_ok()) return maybe_read.error(); if (!bytes_read.is_ok()) return bytes_read.error();
read = maybe_read.value(); read = bytes_read.value();
} }
return written; return total_bytes_written;
} }
/** /**
@ -565,8 +567,8 @@ export namespace coral {
* The returned [expected] can be used to introspect if `output` encountered any issues during * The returned [expected] can be used to introspect if `output` encountered any issues during
* printing, otherwise it will contain the number of characters used to print `value` as text. * printing, otherwise it will contain the number of characters used to print `value` as text.
*/ */
expected<usize, io_error> print_unsigned(writable const & output, u64 value) { expected<usize, io_error> print_unsigned(writer & output, u64 value) {
if (value == 0) return output(slice{"0"}.as_bytes()); if (value == 0) return output.write(slice{"0"}.as_bytes());
u8 buffer[20]{0}; u8 buffer[20]{0};
usize buffer_count{0}; usize buffer_count{0};
@ -584,7 +586,7 @@ export namespace coral {
for (usize i = 0; i < half_buffer_count; i += 1) for (usize i = 0; i < half_buffer_count; i += 1)
swap(buffer[i], buffer[buffer_count - i - 1]); swap(buffer[i], buffer[buffer_count - i - 1]);
return output(slice{buffer, buffer_count}); return output.write(slice{buffer, buffer_count});
} }
/** /**