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

@ -29,15 +29,15 @@ public:
// FIXME: It doesn't make sense to truncate a memory stream. Therefore, we don't do anything here. Is that fine?
virtual ErrorOr<void> truncate(off_t) override { return Error::from_errno(ENOTSUP); }
virtual ErrorOr<size_t> read(Bytes bytes) override
virtual ErrorOr<Bytes> read(Bytes bytes) override
{
auto to_read = min(remaining(), bytes.size());
if (to_read == 0)
return 0;
return Bytes {};
m_bytes.slice(m_offset, to_read).copy_to(bytes);
m_offset += to_read;
return bytes.size();
return bytes.trim(to_read);
}
virtual ErrorOr<off_t> seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override