1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:07:36 +00:00

AK: Add formatters for Span<T> and Span<T const>

This generalizes the formatter currently used for Vector to be usable
for any Span.
This commit is contained in:
Timothy Flynn 2022-12-08 08:08:08 -05:00 committed by Andreas Kling
parent c372012842
commit 949f5460fb
2 changed files with 42 additions and 4 deletions

View file

@ -329,15 +329,16 @@ struct Formatter<StringView> : StandardFormatter {
ErrorOr<void> format(FormatBuilder&, StringView);
};
template<typename T, size_t inline_capacity>
requires(HasFormatter<T>) struct Formatter<Vector<T, inline_capacity>> : StandardFormatter {
template<typename T>
requires(HasFormatter<T>)
struct Formatter<Span<T const>> : StandardFormatter {
Formatter() = default;
explicit Formatter(StandardFormatter formatter)
: StandardFormatter(move(formatter))
{
}
ErrorOr<void> format(FormatBuilder& builder, Vector<T> value)
ErrorOr<void> format(FormatBuilder& builder, Span<T const> value)
{
if (m_mode == Mode::Pointer) {
Formatter<FlatPtr> formatter { *this };
@ -375,6 +376,24 @@ requires(HasFormatter<T>) struct Formatter<Vector<T, inline_capacity>> : Standar
}
};
template<typename T>
requires(HasFormatter<T>)
struct Formatter<Span<T>> : Formatter<Span<T const>> {
ErrorOr<void> format(FormatBuilder& builder, Span<T> value)
{
return Formatter<Span<T const>>::format(builder, value);
}
};
template<typename T, size_t inline_capacity>
requires(HasFormatter<T>)
struct Formatter<Vector<T, inline_capacity>> : Formatter<Span<T const>> {
ErrorOr<void> format(FormatBuilder& builder, Vector<T, inline_capacity> const& value)
{
return Formatter<Span<T const>>::format(builder, value.span());
}
};
template<>
struct Formatter<ReadonlyBytes> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, ReadonlyBytes value)