mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:07:44 +00:00
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:
parent
5f7ac559a7
commit
bbdf766fb0
1 changed files with 16 additions and 0 deletions
16
AK/Span.h
16
AK/Span.h
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue