Skip to content

Commit 202175c

Browse files
timschumiADKaster
andcommitted
LibCore: Add helper functions to read/write trivial values from streams
Co-Authored-By: Andrew Kaster <akaster@serenityos.org>
1 parent 19bae98 commit 202175c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Userland/Libraries/LibCore/Stream.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ class Stream {
108108
return !write_entire_buffer(buffer).is_error();
109109
}
110110

111+
template<typename T>
112+
requires(IsTriviallyDestructible<T>)
113+
ErrorOr<T> read_trivial_value()
114+
{
115+
alignas(T) u8 buffer[sizeof(T)] = {};
116+
TRY(read_entire_buffer(buffer));
117+
return *bit_cast<T>(buffer);
118+
}
119+
120+
template<typename T>
121+
requires(IsTriviallyDestructible<T>)
122+
ErrorOr<void> write_trivial_value(T const& value)
123+
{
124+
return write_entire_buffer({ &value, sizeof(value) });
125+
}
126+
111127
/// Returns whether the stream has reached the end of file. For sockets,
112128
/// this most likely means that the protocol has disconnected (in the case
113129
/// of TCP). For seekable streams, this means the end of the file. Note that

0 commit comments

Comments
 (0)