AK: Add ByteBuffer::{must_,}get_bytes_for_writing()
This is useful for writing new data at the end of a ByteBuffer. For instance, with the Stream API: auto pending_bytes = TRY(stream.pending_bytes()); auto receive_buffer = TRY(buffer.get_bytes_for_writing( pending_bytes)); TRY(stream.read(receive_buffer));
This commit is contained in:
parent
9569841589
commit
28063de488
Notes:
sideshowbarker
2024-07-17 20:58:07 +09:00
Author: https://github.com/sin-ack Commit: https://github.com/SerenityOS/serenity/commit/28063de488a Pull-request: https://github.com/SerenityOS/serenity/pull/11292 Reviewed-by: https://github.com/creator1creeper1 ✅
1 changed files with 14 additions and 0 deletions
|
@ -182,6 +182,20 @@ public:
|
|||
return try_ensure_capacity_slowpath(new_capacity);
|
||||
}
|
||||
|
||||
/// Return a span of bytes past the end of this ByteBuffer for writing.
|
||||
/// Ensures that the required space is available.
|
||||
ErrorOr<Bytes> get_bytes_for_writing(size_t length)
|
||||
{
|
||||
TRY(try_ensure_capacity(size() + length));
|
||||
return Bytes { data() + size(), length };
|
||||
}
|
||||
|
||||
/// Like get_bytes_for_writing, but crashes if allocation fails.
|
||||
Bytes must_get_bytes_for_writing(size_t length)
|
||||
{
|
||||
return MUST(get_bytes_for_writing(length));
|
||||
}
|
||||
|
||||
void append(char byte)
|
||||
{
|
||||
MUST(try_append(byte));
|
||||
|
|
Loading…
Add table
Reference in a new issue