mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09:00
Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr
Apologies for the enormous commit, but I don't see a way to split this up nicely. In the vast majority of cases it's a simple change. A few extra places can use TRY instead of manual error checking though. :^)
This commit is contained in:
parent
140f1d9e55
commit
45cf40653a
Notes:
sideshowbarker
2024-07-17 20:17:25 +09:00
Author: https://github.com/AtkinsSJ
Commit: 45cf40653a
Pull-request: https://github.com/SerenityOS/serenity/pull/12025
79 changed files with 202 additions and 274 deletions
|
@ -61,35 +61,31 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] static Optional<ByteBuffer> create_uninitialized(size_t size)
|
||||
[[nodiscard]] static ErrorOr<ByteBuffer> create_uninitialized(size_t size)
|
||||
{
|
||||
auto buffer = ByteBuffer();
|
||||
if (buffer.try_resize(size).is_error())
|
||||
return {};
|
||||
TRY(buffer.try_resize(size));
|
||||
return { move(buffer) };
|
||||
}
|
||||
|
||||
[[nodiscard]] static Optional<ByteBuffer> create_zeroed(size_t size)
|
||||
[[nodiscard]] static ErrorOr<ByteBuffer> create_zeroed(size_t size)
|
||||
{
|
||||
auto buffer_result = create_uninitialized(size);
|
||||
if (!buffer_result.has_value())
|
||||
return {};
|
||||
auto buffer = TRY(create_uninitialized(size));
|
||||
|
||||
auto& buffer = buffer_result.value();
|
||||
buffer.zero_fill();
|
||||
VERIFY(size == 0 || (buffer[0] == 0 && buffer[size - 1] == 0));
|
||||
return buffer_result;
|
||||
return { move(buffer) };
|
||||
}
|
||||
|
||||
[[nodiscard]] static Optional<ByteBuffer> copy(void const* data, size_t size)
|
||||
[[nodiscard]] static ErrorOr<ByteBuffer> copy(void const* data, size_t size)
|
||||
{
|
||||
auto buffer = create_uninitialized(size);
|
||||
if (buffer.has_value() && size != 0)
|
||||
__builtin_memcpy(buffer->data(), data, size);
|
||||
return buffer;
|
||||
auto buffer = TRY(create_uninitialized(size));
|
||||
if (size != 0)
|
||||
__builtin_memcpy(buffer.data(), data, size);
|
||||
return { move(buffer) };
|
||||
}
|
||||
|
||||
[[nodiscard]] static Optional<ByteBuffer> copy(ReadonlyBytes bytes)
|
||||
[[nodiscard]] static ErrorOr<ByteBuffer> copy(ReadonlyBytes bytes)
|
||||
{
|
||||
return copy(bytes.data(), bytes.size());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue