Fix bugs in coral::fixed_buffer
This commit is contained in:
parent
28eeacfaa9
commit
d8aadef0e7
@ -17,7 +17,7 @@ export namespace coral {
|
|||||||
* [fixed_buffer] is not mutated or out-of-scope.
|
* [fixed_buffer] is not mutated or out-of-scope.
|
||||||
*/
|
*/
|
||||||
slice<u8 const> as_slice() const {
|
slice<u8 const> as_slice() const {
|
||||||
return {0, this->filled};
|
return {this->data, this->data_filled};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,13 +27,6 @@ export namespace coral {
|
|||||||
return this->data;
|
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.
|
* Returns the tail pointer of the buffer data.
|
||||||
*/
|
*/
|
||||||
@ -41,18 +34,25 @@ export namespace coral {
|
|||||||
return this->data + this->cursor;
|
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`.
|
* Returns `true` if the buffer is completely empty of data, otherwise `false`.
|
||||||
*/
|
*/
|
||||||
bool is_empty() const {
|
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`.
|
* Returns `true` if the buffer has been completely filled with data, otherwise `false`.
|
||||||
*/
|
*/
|
||||||
bool is_full() const {
|
bool is_full() const {
|
||||||
return this->filled == capacity;
|
return this->data_filled == capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,7 +62,7 @@ export namespace coral {
|
|||||||
bool put(u8 data) {
|
bool put(u8 data) {
|
||||||
if (this->is_full()) return false;
|
if (this->is_full()) return false;
|
||||||
|
|
||||||
this->filled += 1;
|
this->data_filled += 1;
|
||||||
this->data[this->write_index] = data;
|
this->data[this->write_index] = data;
|
||||||
this->write_index = (this->write_index + 1) % capacity;
|
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.
|
* 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 {
|
expected<usize, io_error> read(slice<u8> 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) {
|
for (usize index = 0; index < readable_data.length; index += 1) {
|
||||||
data[index] = this->data[this->read_index];
|
data[index] = this->data[this->read_index];
|
||||||
@ -85,6 +85,10 @@ export namespace coral {
|
|||||||
return readable_data.length;
|
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
|
* 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.
|
* it has been completely filled and no more bytes can be written.
|
||||||
@ -92,9 +96,9 @@ export namespace coral {
|
|||||||
expected<usize, io_error> write(slice<u8 const> const & data) override {
|
expected<usize, io_error> write(slice<u8 const> const & data) override {
|
||||||
if (this->is_full()) return io_error::unavailable;
|
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) {
|
for (usize index = 0; index < writable_data.length; index += 1) {
|
||||||
this->data[this->write_index] = data[index];
|
this->data[this->write_index] = data[index];
|
||||||
@ -105,7 +109,7 @@ export namespace coral {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
usize filled {0};
|
usize data_filled {0};
|
||||||
|
|
||||||
usize read_index {0};
|
usize read_index {0};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user