From cf8124eb20bd924ad4d1dd12025dce89c5dababd Mon Sep 17 00:00:00 2001 From: kayomn Date: Thu, 23 Feb 2023 00:51:48 +0000 Subject: [PATCH] Expose interface for writing single values into coral::fixed_buffer --- source/coral/io.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/source/coral/io.cpp b/source/coral/io.cpp index c2b595e..b512c09 100644 --- a/source/coral/io.cpp +++ b/source/coral/io.cpp @@ -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 read(slice const & data) override { + slice const readable_data{this->data, min(this->filled, data.length)}; this->filled -= readable_data.length;