Expose interface for writing single values into coral::fixed_buffer

This commit is contained in:
kayomn 2023-02-23 00:51:48 +00:00
parent 2b1a34d5e2
commit cf8124eb20
1 changed files with 15 additions and 0 deletions

View File

@ -55,11 +55,26 @@ export namespace coral {
return this->filled == capacity;
}
/**
* Attempts to write the single value `data` into the buffer, returning `true` if
* successful, otherwise `false` if the buffer is full.
*/
bool put(u8 data) {
if (this->is_full()) return false;
this->filled += 1;
this->data[this->write_index] = data;
this->write_index = (this->write_index + 1) % capacity;
return true;
}
/**
* Reads whatever data is in the buffer into `data`, returning the number of bytes read
* from the buffer.
*/
expected<usize, io_error> read(slice<u8> const & data) override {
slice const readable_data{this->data, min(this->filled, data.length)};
this->filled -= readable_data.length;