mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 18:20:43 +09:00
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.
This commit is contained in:
parent
8b5b767465
commit
6ccda76a71
Notes:
sideshowbarker
2024-07-17 01:20:27 +09:00
Author: https://github.com/timschumi
Commit: 6ccda76a71
Pull-request: https://github.com/SerenityOS/serenity/pull/17094
1 changed files with 14 additions and 0 deletions
|
@ -101,6 +101,13 @@ public:
|
|||
/// contents are written or an error occurs.
|
||||
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes);
|
||||
|
||||
template<typename T>
|
||||
requires(requires(Stream& stream) { { T::read_from_stream(stream) } -> SameAs<ErrorOr<T>>; })
|
||||
ErrorOr<T> read_value()
|
||||
{
|
||||
return T::read_from_stream(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires(Traits<T>::is_trivially_serializable())
|
||||
ErrorOr<T> read_value()
|
||||
|
@ -110,6 +117,13 @@ public:
|
|||
return bit_cast<T>(buffer);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires(requires(T t, Stream& stream) { { t.write_to_stream(stream) } -> SameAs<ErrorOr<void>>; })
|
||||
ErrorOr<void> write_value(T const& value)
|
||||
{
|
||||
return value.write_to_stream(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires(Traits<T>::is_trivially_serializable())
|
||||
ErrorOr<void> write_value(T const& value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue