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;