1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:47:35 +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

@ -30,16 +30,16 @@ ErrorOr<void> Client::drain_socket()
auto buffer = TRY(ByteBuffer::create_uninitialized(1024));
while (TRY(m_socket->can_read_without_blocking())) {
auto nread = TRY(m_socket->read(buffer));
auto bytes_read = TRY(m_socket->read(buffer));
dbgln("Read {} bytes.", nread);
dbgln("Read {} bytes.", bytes_read.size());
if (m_socket->is_eof()) {
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
break;
}
TRY(m_socket->write({ buffer.data(), nread }));
TRY(m_socket->write(bytes_read));
}
return {};