From 8b2f23d0166ec9d21a70f10dadb2d7533a161079 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Mon, 30 Jan 2023 11:05:12 +0100 Subject: [PATCH] AK: Remove the fallible constructor from `LittleEndianOutputBitStream` --- AK/BitStream.h | 9 ++------- Tests/AK/TestBitStream.cpp | 20 ++++++++++---------- Userland/Libraries/LibCompress/Deflate.cpp | 2 +- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/AK/BitStream.h b/AK/BitStream.h index 310217e0e1..c5a3d89801 100644 --- a/AK/BitStream.h +++ b/AK/BitStream.h @@ -303,9 +303,9 @@ private: /// in little-endian order to another stream. class LittleEndianOutputBitStream : public Stream { public: - static ErrorOr> construct(MaybeOwned stream) + explicit LittleEndianOutputBitStream(MaybeOwned stream) + : m_stream(move(stream)) { - return adopt_nonnull_own_or_enomem(new LittleEndianOutputBitStream(move(stream))); } virtual ErrorOr read(Bytes) override @@ -372,11 +372,6 @@ public: } private: - LittleEndianOutputBitStream(MaybeOwned stream) - : m_stream(move(stream)) - { - } - MaybeOwned m_stream; u8 m_current_byte { 0 }; size_t m_bit_offset { 0 }; diff --git a/Tests/AK/TestBitStream.cpp b/Tests/AK/TestBitStream.cpp index 5e99f61bd2..06ffa0a574 100644 --- a/Tests/AK/TestBitStream.cpp +++ b/Tests/AK/TestBitStream.cpp @@ -15,21 +15,21 @@ TEST_CASE(little_endian_bit_stream_input_output_match) // Note: The bit stream only ever reads from/writes to the underlying stream in one byte chunks, // so testing with sizes that will not trigger a write will yield unexpected results. - auto bit_write_stream = MUST(LittleEndianOutputBitStream::construct(MaybeOwned(*memory_stream))); + LittleEndianOutputBitStream bit_write_stream { MaybeOwned(*memory_stream) }; LittleEndianInputBitStream bit_read_stream { MaybeOwned(*memory_stream) }; // Test two mirrored chunks of a fully mirrored pattern to check that we are not dropping bits. { - MUST(bit_write_stream->write_bits(0b1111u, 4)); - MUST(bit_write_stream->write_bits(0b1111u, 4)); + MUST(bit_write_stream.write_bits(0b1111u, 4)); + MUST(bit_write_stream.write_bits(0b1111u, 4)); auto result = MUST(bit_read_stream.read_bits(4)); EXPECT_EQ(0b1111u, result); result = MUST(bit_read_stream.read_bits(4)); EXPECT_EQ(0b1111u, result); } { - MUST(bit_write_stream->write_bits(0b0000u, 4)); - MUST(bit_write_stream->write_bits(0b0000u, 4)); + MUST(bit_write_stream.write_bits(0b0000u, 4)); + MUST(bit_write_stream.write_bits(0b0000u, 4)); auto result = MUST(bit_read_stream.read_bits(4)); EXPECT_EQ(0b0000u, result); result = MUST(bit_read_stream.read_bits(4)); @@ -38,8 +38,8 @@ TEST_CASE(little_endian_bit_stream_input_output_match) // Test two mirrored chunks of a non-mirrored pattern to check that we are writing bits within a pattern in the correct order. { - MUST(bit_write_stream->write_bits(0b1000u, 4)); - MUST(bit_write_stream->write_bits(0b1000u, 4)); + MUST(bit_write_stream.write_bits(0b1000u, 4)); + MUST(bit_write_stream.write_bits(0b1000u, 4)); auto result = MUST(bit_read_stream.read_bits(4)); EXPECT_EQ(0b1000u, result); result = MUST(bit_read_stream.read_bits(4)); @@ -48,8 +48,8 @@ TEST_CASE(little_endian_bit_stream_input_output_match) // Test two different chunks to check that we are not confusing their order. { - MUST(bit_write_stream->write_bits(0b1000u, 4)); - MUST(bit_write_stream->write_bits(0b0100u, 4)); + MUST(bit_write_stream.write_bits(0b1000u, 4)); + MUST(bit_write_stream.write_bits(0b0100u, 4)); auto result = MUST(bit_read_stream.read_bits(4)); EXPECT_EQ(0b1000u, result); result = MUST(bit_read_stream.read_bits(4)); @@ -58,7 +58,7 @@ TEST_CASE(little_endian_bit_stream_input_output_match) // Test a pattern that spans multiple bytes. { - MUST(bit_write_stream->write_bits(0b1101001000100001u, 16)); + MUST(bit_write_stream.write_bits(0b1101001000100001u, 16)); auto result = MUST(bit_read_stream.read_bits(16)); EXPECT_EQ(0b1101001000100001u, result); } diff --git a/Userland/Libraries/LibCompress/Deflate.cpp b/Userland/Libraries/LibCompress/Deflate.cpp index 18ad215645..f6c296635f 100644 --- a/Userland/Libraries/LibCompress/Deflate.cpp +++ b/Userland/Libraries/LibCompress/Deflate.cpp @@ -449,7 +449,7 @@ ErrorOr DeflateDecompressor::decode_codes(CanonicalCode& literal_code, Opt ErrorOr> DeflateCompressor::construct(MaybeOwned stream, CompressionLevel compression_level) { - auto bit_stream = TRY(LittleEndianOutputBitStream::construct(move(stream))); + auto bit_stream = TRY(try_make(move(stream))); auto deflate_compressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) DeflateCompressor(move(bit_stream), compression_level))); return deflate_compressor; }