1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:27:34 +00:00

LibCore+Everywhere: Make Core::Stream::read() return Bytes

A mistake I've repeatedly made is along these lines:
```c++
auto nread = TRY(source_file->read(buffer));
TRY(destination_file->write(buffer));
```

It's a little clunky to have to create a Bytes or StringView from the
buffer's data pointer and the nread, and easy to forget and just use
the buffer. So, this patch changes the read() function to return a
Bytes of the data that were just read.

The other read_foo() methods will be modified in the same way in
subsequent commits.

Fixes #13687
This commit is contained in:
Sam Atkins 2022-04-15 13:33:02 +01:00 committed by Tim Flynn
parent 6654efcd82
commit 3b1e063d30
22 changed files with 103 additions and 103 deletions

View file

@ -238,15 +238,15 @@ ErrorOr<int> execute_work_items(Vector<WorkItem> const& items)
while (true) {
print_progress();
auto bytes_read = TRY(source_file->read(buffer.bytes()));
if (bytes_read == 0)
if (bytes_read.is_empty())
break;
if (auto result = destination_file->write(buffer); result.is_error()) {
if (auto result = destination_file->write(bytes_read); result.is_error()) {
// FIXME: Return the formatted string directly. There is no way to do this right now without the temporary going out of scope and being destroyed.
report_warning(String::formatted("Failed to write to destination file: {}", result.error()));
return result.error();
}
item_done += bytes_read;
executed_work_bytes += bytes_read;
item_done += bytes_read.size();
executed_work_bytes += bytes_read.size();
print_progress();
// FIXME: Remove this once the kernel is smart enough to schedule other threads
// while we're doing heavy I/O. Right now, copying a large file will totally