Replace coral::readable / writable with coral::reader / writer

This commit is contained in:
kayomn 2023-02-19 23:06:17 +00:00
parent c45a270a0b
commit 9ac61b614f
2 changed files with 31 additions and 13 deletions

View File

@ -106,18 +106,31 @@ export namespace coral {
char buffer[max + 1];
};
struct file_reader : public reader {
virtual expected<u64, io_error> seek(u64 offset) = 0;
virtual expected<u64, io_error> tell() = 0;
};
struct file_writer : public writer {};
/**
* Platform-generalized file system interface.
*/
struct fs {
struct io_rules {
bool can_read;
bool can_write;
};
/**
* Attempts to read the file in the file system located at `file_path` relative, calling
* `then` if it was successfully opened for reading.
*
* Once `then` returns, access to the file is closed automatically.
*/
virtual void read_file(path const & file_path,
callable<void(readable const &)> const & then) = 0;
virtual void read_file(path const & file_path, callable<void(file_reader &)> const & then) = 0;
/**
* Attempts to write the file in the file system located at `file_path` relative, calling
@ -125,7 +138,6 @@ export namespace coral {
*
* Once `then` returns, access to the file is closed automatically.
*/
virtual void write_file(path const & file_path,
callable<void(writable const &)> const & then) = 0;
virtual void write_file(path const & file_path, callable<void(file_writer &)> const & then) = 0;
};
}

View File

@ -200,14 +200,20 @@ export namespace coral {
/**
* Writable type for appending data to a [sequence] containing [u8] values.
*/
struct sequence_writer : public writable {
sequence_writer(sequence<u8> * output_sequence) : writable{
[output_sequence](slice<u8 const> const & buffer) -> expected<usize, io_error> {
struct sequence_writer : public writer {
sequence_writer(sequence<u8> * output_sequence) {
this->output_sequence = output_sequence;
}
expected<usize, io_error> write(slice<u8 const> const & buffer) {
switch (output_sequence->append(buffer)) {
case append_result::ok: return buffer.length;
case append_result::out_of_memory: return io_error::unavailable;
default: unreachable();
}
}} {}
}
private:
sequence<u8> * output_sequence;
};
}