mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +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
Notes:
sideshowbarker
2024-07-17 03:19:14 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/bbdf766fb0 Pull-request: https://github.com/SerenityOS/serenity/pull/22021
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…
Reference in a new issue