Skip to content

Commit

Permalink
LibCore: Remove Stream::is_{readable,writable}
Browse files Browse the repository at this point in the history
Next to functions like `is_eof` these were really confusing to use, and
the `read`/`write` functions should fail anyways if a stream is not
readable/writable.
  • Loading branch information
timschumi authored and awesomekling committed Dec 12, 2022
1 parent 5a346c4 commit 5061a90
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 32 deletions.
1 change: 0 additions & 1 deletion Meta/Lagom/Wasm/js_repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ void displayln() { user_display("\n", 1); }

class UserDisplayStream final : public Core::Stream::Stream {
virtual ErrorOr<Bytes> read(Bytes) override { return Error::from_string_view("Not readable"sv); };
virtual bool is_writable() const override { return true; }
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override
{
user_display(bit_cast<char const*>(bytes.data()), bytes.size());
Expand Down
2 changes: 0 additions & 2 deletions Tests/LibCore/TestLibCoreStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ TEST_CASE(file_open)
// Testing out some basic file properties.
auto file = maybe_file.release_value();
EXPECT(file->is_open());
EXPECT(!file->is_readable());
EXPECT(file->is_writable());
EXPECT(!file->is_eof());

auto maybe_size = file->size();
Expand Down
2 changes: 0 additions & 2 deletions Userland/Libraries/LibCompress/Brotli.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ class BrotliDecompressionStream : public Stream {
public:
BrotliDecompressionStream(Stream&);

bool is_readable() const override { return m_input_stream.is_readable(); }
ErrorOr<Bytes> read(Bytes output_buffer) override;
bool is_writable() const override { return m_input_stream.is_writable(); }
ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_input_stream.write(bytes); }
bool is_eof() const override;
bool is_open() const override { return m_input_stream.is_open(); }
Expand Down
4 changes: 0 additions & 4 deletions Userland/Libraries/LibCore/InputBitStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class BigEndianInputBitStream : public Stream {
}

// ^Stream
virtual bool is_readable() const override { return m_stream.is_readable(); }
virtual ErrorOr<Bytes> read(Bytes bytes) override
{
if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) {
Expand All @@ -40,7 +39,6 @@ class BigEndianInputBitStream : public Stream {
align_to_byte_boundary();
return m_stream.read(bytes);
}
virtual bool is_writable() const override { return m_stream.is_writable(); }
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream.write(bytes); }
virtual bool write_or_error(ReadonlyBytes bytes) override { return m_stream.write_or_error(bytes); }
virtual bool is_eof() const override { return m_stream.is_eof() && !m_current_byte.has_value(); }
Expand Down Expand Up @@ -147,7 +145,6 @@ class LittleEndianInputBitStream : public Stream {
}

// ^Stream
virtual bool is_readable() const override { return m_stream.is_readable(); }
virtual ErrorOr<Bytes> read(Bytes bytes) override
{
if (m_current_byte.has_value() && is_aligned_to_byte_boundary()) {
Expand All @@ -157,7 +154,6 @@ class LittleEndianInputBitStream : public Stream {
align_to_byte_boundary();
return m_stream.read(bytes);
}
virtual bool is_writable() const override { return m_stream.is_writable(); }
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override { return m_stream.write(bytes); }
virtual bool write_or_error(ReadonlyBytes bytes) override { return m_stream.write_or_error(bytes); }
virtual bool is_eof() const override { return m_stream.is_eof() && !m_current_byte.has_value(); }
Expand Down
3 changes: 0 additions & 3 deletions Userland/Libraries/LibCore/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@ ErrorOr<void> File::open_path(StringView filename, mode_t permissions)
return {};
}

bool File::is_readable() const { return has_flag(m_mode, OpenMode::Read); }
bool File::is_writable() const { return has_flag(m_mode, OpenMode::Write); }

ErrorOr<Bytes> File::read(Bytes buffer)
{
if (!has_flag(m_mode, OpenMode::Read)) {
Expand Down
18 changes: 1 addition & 17 deletions Userland/Libraries/LibCore/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace Core::Stream {
/// operations one can perform on every stream in LibCore.
class Stream {
public:
virtual bool is_readable() const { return false; }
/// Reads into a buffer, with the maximum size being the size of the buffer.
/// The amount of bytes read can be smaller than the size of the buffer.
/// Returns either the bytes that were read, or an errno in the case of
Expand All @@ -46,7 +45,6 @@ class Stream {
/// internal stack-based buffer.
virtual ErrorOr<void> discard(size_t discarded_bytes);

virtual bool is_writable() const { return false; }
/// Tries to write the entire contents of the buffer. It is possible for
/// less than the full buffer to be written. Returns either the amount of
/// bytes written into the stream, or an errno in the case of failure.
Expand Down Expand Up @@ -239,10 +237,8 @@ class File final : public SeekableStream {
return *this;
}

virtual bool is_readable() const override;
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<ByteBuffer> read_all(size_t block_size = 4096) override;
virtual bool is_writable() const override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
Expand Down Expand Up @@ -346,8 +342,6 @@ class TCPSocket final : public Socket {
return *this;
}

virtual bool is_readable() const override { return is_open(); }
virtual bool is_writable() const override { return is_open(); }
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
virtual bool is_eof() const override { return m_helper.is_eof(); }
Expand Down Expand Up @@ -423,8 +417,6 @@ class UDPSocket final : public Socket {
return m_helper.read(buffer, default_flags());
}

virtual bool is_readable() const override { return is_open(); }
virtual bool is_writable() const override { return is_open(); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
virtual bool is_eof() const override { return m_helper.is_eof(); }
virtual bool is_open() const override { return m_helper.is_open(); }
Expand Down Expand Up @@ -484,8 +476,6 @@ class LocalSocket final : public Socket {
return *this;
}

virtual bool is_readable() const override { return is_open(); }
virtual bool is_writable() const override { return is_open(); }
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
virtual bool is_eof() const override { return m_helper.is_eof(); }
Expand Down Expand Up @@ -717,7 +707,7 @@ class BufferedHelper {
if (m_buffer.span().slice(0, m_buffered_size).contains_slow('\n'))
return true;

if (!stream().is_readable())
if (stream().is_eof())
return false;

while (m_buffered_size < m_buffer.size()) {
Expand Down Expand Up @@ -818,9 +808,7 @@ class BufferedSeekable final : public SeekableStream {
BufferedSeekable(BufferedSeekable&& other) = default;
BufferedSeekable& operator=(BufferedSeekable&& other) = default;

virtual bool is_readable() const override { return m_helper.stream().is_readable(); }
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(move(buffer)); }
virtual bool is_writable() const override { return m_helper.stream().is_writable(); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.stream().write(buffer); }
virtual bool is_eof() const override { return m_helper.is_eof(); }
virtual bool is_open() const override { return m_helper.stream().is_open(); }
Expand Down Expand Up @@ -889,9 +877,7 @@ class BufferedSocket final : public BufferedSocketBase {
return *this;
}

virtual bool is_readable() const override { return m_helper.stream().is_readable(); }
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(move(buffer)); }
virtual bool is_writable() const override { return m_helper.stream().is_writable(); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.stream().write(buffer); }
virtual bool is_eof() const override { return m_helper.is_eof(); }
virtual bool is_open() const override { return m_helper.stream().is_open(); }
Expand Down Expand Up @@ -977,9 +963,7 @@ class BasicReusableSocket final : public ReusableSocket {
return {};
}

virtual bool is_readable() const override { return m_socket.is_readable(); }
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_socket.read(move(buffer)); }
virtual bool is_writable() const override { return m_socket.is_writable(); }
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_socket.write(buffer); }
virtual bool is_eof() const override { return m_socket.is_eof(); }
virtual bool is_open() const override { return m_socket.is_open(); }
Expand Down
3 changes: 0 additions & 3 deletions Userland/Libraries/LibTLS/TLSv12.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,6 @@ class TLSv12 final : public Core::Stream::Socket {
}

public:
virtual bool is_readable() const override { return true; }
virtual bool is_writable() const override { return true; }

/// Reads into a buffer, with the maximum size being the size of the buffer.
/// The amount of bytes read can be smaller than the size of the buffer.
/// Returns either the bytes that were read, or an errno in the case of
Expand Down

0 comments on commit 5061a90

Please sign in to comment.