diff --git a/source/coral/io.cpp b/source/coral/io.cpp index 8741307..1de6ca0 100755 --- a/source/coral/io.cpp +++ b/source/coral/io.cpp @@ -17,7 +17,7 @@ export namespace coral { * [fixed_buffer] is not mutated or out-of-scope. */ slice as_slice() const { - return {0, this->filled}; + return {this->data, this->data_filled}; } /** @@ -27,13 +27,6 @@ export namespace coral { return this->data; } - /** - * Returns the number of bytes in the buffer that have been filled with data. - */ - usize count() const { - return this->filled; - } - /** * Returns the tail pointer of the buffer data. */ @@ -41,18 +34,25 @@ export namespace coral { return this->data + this->cursor; } + /** + * Returns the number of bytes in the buffer that have been filled with data. + */ + usize filled() const { + return this->data_filled; + } + /** * Returns `true` if the buffer is completely empty of data, otherwise `false`. */ bool is_empty() const { - return this->filled == capacity; + return this->data_filled == capacity; } /** * Returns `true` if the buffer has been completely filled with data, otherwise `false`. */ bool is_full() const { - return this->filled == capacity; + return this->data_filled == capacity; } /** @@ -62,7 +62,7 @@ export namespace coral { bool put(u8 data) { if (this->is_full()) return false; - this->filled += 1; + this->data_filled += 1; this->data[this->write_index] = data; this->write_index = (this->write_index + 1) % capacity; @@ -73,9 +73,9 @@ export namespace coral { * 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)}; + slice const readable_data {this->data, min(this->data_filled, data.length)}; - this->filled -= readable_data.length; + this->data_filled -= readable_data.length; for (usize index = 0; index < readable_data.length; index += 1) { data[index] = this->data[this->read_index]; @@ -85,6 +85,10 @@ export namespace coral { return readable_data.length; } + usize remaining() const { + return capacity - this->data_filled; + } + /** * Attempts to write `data` to the buffer, returning the number of bytes written or [io_error::unavailable] if * it has been completely filled and no more bytes can be written. @@ -92,9 +96,9 @@ export namespace coral { expected write(slice const & data) override { if (this->is_full()) return io_error::unavailable; - slice const writable_data{data.sliced(0, min(data.length, this->filled))}; + slice const writable_data {data.sliced(0, min(data.length, this->remaining()))}; - this->filled += writable_data.length; + this->data_filled += writable_data.length; for (usize index = 0; index < writable_data.length; index += 1) { this->data[this->write_index] = data[index]; @@ -105,7 +109,7 @@ export namespace coral { } private: - usize filled {0}; + usize data_filled {0}; usize read_index {0};