mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 16:18:12 +00:00
AK: Add FixedMemoryStream methods for reading values "in place"
When working with FixedMemoryStreams, and especially MappedFiles, you may don't want to copy the underlying data when you read from the stream. Pointing into that data is perfectly fine as long as you know the lifetime of it is long enough. This commit adds a couple of methods for reading either a single value, or a span of them, in this way. As noted, for single values you sadly get a raw pointer instead of a reference, but that's the only option right now.
This commit is contained in:
parent
8e51c7112c
commit
253a96277e
2 changed files with 62 additions and 0 deletions
|
@ -243,3 +243,30 @@ TEST_CASE(fixed_memory_truncate)
|
|||
|
||||
EXPECT(stream.truncate(999).is_error());
|
||||
}
|
||||
|
||||
TEST_CASE(fixed_memory_read_in_place)
|
||||
{
|
||||
constexpr auto some_words = "These are some words"sv;
|
||||
|
||||
FixedMemoryStream readonly_stream { ReadonlyBytes { some_words.bytes() } };
|
||||
|
||||
// Trying to read mutable values from a read-only stream should fail.
|
||||
EXPECT(readonly_stream.read_in_place<u8>(1).is_error());
|
||||
EXPECT_EQ(readonly_stream.offset(), 0u);
|
||||
|
||||
// Reading const values should succeed.
|
||||
auto characters = TRY_OR_FAIL(readonly_stream.read_in_place<u8 const>(20));
|
||||
EXPECT_EQ(characters, some_words.bytes());
|
||||
EXPECT(readonly_stream.is_eof());
|
||||
|
||||
FixedMemoryStream mutable_stream { Bytes { const_cast<u8*>(some_words.bytes().data()), some_words.bytes().size() }, true };
|
||||
// Trying to read mutable values from a mutable stream should succeed.
|
||||
TRY_OR_FAIL(mutable_stream.read_in_place<u8>(1));
|
||||
EXPECT_EQ(mutable_stream.offset(), 1u);
|
||||
TRY_OR_FAIL(mutable_stream.seek(0));
|
||||
|
||||
// Reading const values should succeed.
|
||||
auto characters_again = TRY_OR_FAIL(mutable_stream.read_in_place<u8 const>(20));
|
||||
EXPECT_EQ(characters_again, some_words.bytes());
|
||||
EXPECT(mutable_stream.is_eof());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue