mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibCore: Add helper functions to read/write trivial values from streams
Co-Authored-By: Andrew Kaster <akaster@serenityos.org>
This commit is contained in:
parent
19bae983e4
commit
202175cf4c
Notes:
sideshowbarker
2024-07-17 01:44:21 +09:00
Author: https://github.com/timschumi Commit: https://github.com/SerenityOS/serenity/commit/202175cf4c Pull-request: https://github.com/SerenityOS/serenity/pull/16992 Reviewed-by: https://github.com/ADKaster ✅
1 changed files with 16 additions and 0 deletions
|
@ -108,6 +108,22 @@ public:
|
|||
return !write_entire_buffer(buffer).is_error();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires(IsTriviallyDestructible<T>)
|
||||
ErrorOr<T> read_trivial_value()
|
||||
{
|
||||
alignas(T) u8 buffer[sizeof(T)] = {};
|
||||
TRY(read_entire_buffer(buffer));
|
||||
return *bit_cast<T>(buffer);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
requires(IsTriviallyDestructible<T>)
|
||||
ErrorOr<void> write_trivial_value(T const& value)
|
||||
{
|
||||
return write_entire_buffer({ &value, sizeof(value) });
|
||||
}
|
||||
|
||||
/// Returns whether the stream has reached the end of file. For sockets,
|
||||
/// this most likely means that the protocol has disconnected (in the case
|
||||
/// of TCP). For seekable streams, this means the end of the file. Note that
|
||||
|
|
Loading…
Reference in a new issue