AK: Add helpers to convert arbitrary Spans to {Readonly}Bytes

The streams and other common APIs require byte spans to operate on
arbitrary data. This is less than helpful when wanting to serialize
spans of other data types, such as from an Array or Vector of u32s.
This commit is contained in:
Andrew Kaster 2023-11-22 09:41:12 -07:00 committed by Andreas Kling
parent 5f7ac559a7
commit bbdf766fb0
Notes: sideshowbarker 2024-07-17 03:19:14 +09:00

View file

@ -322,6 +322,20 @@ using ReadonlySpan = Span<T const>;
using ReadonlyBytes = ReadonlySpan<u8>;
using Bytes = Span<u8>;
template<typename T>
requires(IsTrivial<T>)
ReadonlyBytes to_readonly_bytes(Span<T> span)
{
return ReadonlyBytes { static_cast<void*>(span.data()), span.size() * sizeof(T) };
}
template<typename T>
requires(IsTrivial<T> && !IsConst<T>)
Bytes to_bytes(Span<T> span)
{
return Bytes { static_cast<void*>(span.data()), span.size() * sizeof(T) };
}
}
#if USING_AK_GLOBALLY
@ -329,4 +343,6 @@ using AK::Bytes;
using AK::ReadonlyBytes;
using AK::ReadonlySpan;
using AK::Span;
using AK::to_bytes;
using AK::to_readonly_bytes;
#endif