From 6ccda76a7161a705aa83d35da970f10807728cd6 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Thu, 19 Jan 2023 10:27:19 +0100 Subject: [PATCH] LibCore: Add support for non-trivial types to `Stream::*_value` At the moment, there is no immediate advantage compared to just calling the underlying functions directly, but having a common interface feels more ergonomic (users don't have to care about how a type serializes) and maybe we'll find a way to hide the actual implementation from direct access some time in the future. --- Userland/Libraries/LibCore/Stream.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index 7e3cf6f23a..1351a41a8a 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -101,6 +101,13 @@ public: /// contents are written or an error occurs. virtual ErrorOr write_entire_buffer(ReadonlyBytes); + template + requires(requires(Stream& stream) { { T::read_from_stream(stream) } -> SameAs>; }) + ErrorOr read_value() + { + return T::read_from_stream(*this); + } + template requires(Traits::is_trivially_serializable()) ErrorOr read_value() @@ -110,6 +117,13 @@ public: return bit_cast(buffer); } + template + requires(requires(T t, Stream& stream) { { t.write_to_stream(stream) } -> SameAs>; }) + ErrorOr write_value(T const& value) + { + return value.write_to_stream(*this); + } + template requires(Traits::is_trivially_serializable()) ErrorOr write_value(T const& value)