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

AK: Convert AK::Format formatting helpers to returning ErrorOr<void>

This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
This commit is contained in:
Andreas Kling 2021-11-16 01:15:21 +01:00
parent 008355c222
commit 216e21a1fa
87 changed files with 450 additions and 448 deletions

View file

@ -143,7 +143,7 @@ private:
template<>
struct AK::Formatter<IOAddress> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, IOAddress value)
ErrorOr<void> format(FormatBuilder& builder, IOAddress value)
{
return Formatter<FormatString>::format(builder, "IO {:x}", value.get());
}

View file

@ -298,7 +298,7 @@ class Device;
template<>
struct AK::Formatter<Kernel::PCI::Address> : Formatter<FormatString> {
void format(FormatBuilder& builder, Kernel::PCI::Address value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::PCI::Address value)
{
return Formatter<FormatString>::format(
builder,
@ -308,7 +308,7 @@ struct AK::Formatter<Kernel::PCI::Address> : Formatter<FormatString> {
template<>
struct AK::Formatter<Kernel::PCI::HardwareID> : Formatter<FormatString> {
void format(FormatBuilder& builder, Kernel::PCI::HardwareID value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::PCI::HardwareID value)
{
return Formatter<FormatString>::format(
builder,

View file

@ -53,7 +53,7 @@ private:
template<>
struct AK::Formatter<Kernel::InodeIdentifier> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, Kernel::InodeIdentifier value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::InodeIdentifier value)
{
return AK::Formatter<FormatString>::format(builder, "{}:{}", value.fsid(), value.index());
}

View file

@ -37,7 +37,7 @@ public:
// FIXME: This really not ideal, but vformat expects StringBuilder.
StringBuilder builder;
AK::VariadicFormatParams variadic_format_params { parameters... };
vformat(builder, fmtstr.view(), variadic_format_params);
TRY(vformat(builder, fmtstr.view(), variadic_format_params));
return append_bytes(builder.string_view().bytes());
}

View file

@ -46,28 +46,27 @@ namespace AK {
template<>
struct Formatter<Kernel::KString> : Formatter<StringView> {
void format(FormatBuilder& builder, Kernel::KString const& value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::KString const& value)
{
Formatter<StringView>::format(builder, value.view());
return Formatter<StringView>::format(builder, value.view());
}
};
template<>
struct Formatter<OwnPtr<Kernel::KString>> : Formatter<StringView> {
void format(FormatBuilder& builder, OwnPtr<Kernel::KString> const& value)
ErrorOr<void> format(FormatBuilder& builder, OwnPtr<Kernel::KString> const& value)
{
if (value)
Formatter<StringView>::format(builder, value->view());
else
Formatter<StringView>::format(builder, "[out of memory]"sv);
return Formatter<StringView>::format(builder, value->view());
return Formatter<StringView>::format(builder, "[out of memory]"sv);
}
};
template<>
struct Formatter<NonnullOwnPtr<Kernel::KString>> : Formatter<StringView> {
void format(FormatBuilder& builder, NonnullOwnPtr<Kernel::KString> const& value)
ErrorOr<void> format(FormatBuilder& builder, NonnullOwnPtr<Kernel::KString> const& value)
{
Formatter<StringView>::format(builder, value->view());
return Formatter<StringView>::format(builder, value->view());
}
};

View file

@ -322,9 +322,9 @@ inline NonnullRefPtr<T> adopt_ref(T& object)
template<typename T>
struct Formatter<NonnullRefPtr<T>> : Formatter<const T*> {
void format(FormatBuilder& builder, const NonnullRefPtr<T>& value)
ErrorOr<void> format(FormatBuilder& builder, const NonnullRefPtr<T>& value)
{
Formatter<const T*>::format(builder, value.ptr());
return Formatter<const T*>::format(builder, value.ptr());
}
};

View file

@ -445,9 +445,9 @@ private:
template<typename T>
struct Formatter<RefPtr<T>> : Formatter<const T*> {
void format(FormatBuilder& builder, const RefPtr<T>& value)
ErrorOr<void> format(FormatBuilder& builder, RefPtr<T> const& value)
{
Formatter<const T*>::format(builder, value.ptr());
return Formatter<const T*>::format(builder, value.ptr());
}
};

View file

@ -217,13 +217,13 @@ inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
template<typename T>
struct Formatter<WeakPtr<T>> : Formatter<const T*> {
void format(FormatBuilder& builder, const WeakPtr<T>& value)
ErrorOr<void> format(FormatBuilder& builder, WeakPtr<T> const& value)
{
#ifdef KERNEL
auto ref = value.strong_ref();
Formatter<const T*>::format(builder, ref.ptr());
return Formatter<const T*>::format(builder, ref.ptr());
#else
Formatter<const T*>::format(builder, value.ptr());
return Formatter<const T*>::format(builder, value.ptr());
#endif
}
};

View file

@ -62,7 +62,7 @@ private:
template<>
struct AK::Formatter<Kernel::Memory::VirtualRange> : Formatter<FormatString> {
void format(FormatBuilder& builder, Kernel::Memory::VirtualRange value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::Memory::VirtualRange value)
{
return Formatter<FormatString>::format(builder, "{} - {} (size {:p})", value.base().as_ptr(), value.base().offset(value.size() - 1).as_ptr(), value.size());
}

View file

@ -58,7 +58,7 @@ private:
template<>
struct AK::Formatter<PhysicalAddress> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, PhysicalAddress value)
ErrorOr<void> format(FormatBuilder& builder, PhysicalAddress value)
{
if constexpr (sizeof(PhysicalPtr) == sizeof(u64))
return AK::Formatter<FormatString>::format(builder, "P{:016x}", value.get());

View file

@ -980,7 +980,7 @@ inline static ErrorOr<NonnullOwnPtr<KString>> try_copy_kstring_from_user(const K
template<>
struct AK::Formatter<Kernel::Process> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, const Kernel::Process& value)
ErrorOr<void> format(FormatBuilder& builder, Kernel::Process const& value)
{
return AK::Formatter<FormatString>::format(builder, "{}({})", value.name(), value.pid().value());
}

View file

@ -1279,7 +1279,7 @@ void Thread::track_lock_release(LockRank rank)
}
void AK::Formatter<Kernel::Thread>::format(FormatBuilder& builder, const Kernel::Thread& value)
ErrorOr<void> AK::Formatter<Kernel::Thread>::format(FormatBuilder& builder, Kernel::Thread const& value)
{
return AK::Formatter<FormatString>::format(
builder,

View file

@ -1432,5 +1432,5 @@ inline IterationDecision Thread::for_each_in_state(State state, Callback callbac
template<>
struct AK::Formatter<Kernel::Thread> : AK::Formatter<FormatString> {
void format(FormatBuilder&, const Kernel::Thread&);
ErrorOr<void> format(FormatBuilder&, Kernel::Thread const&);
};

View file

@ -54,7 +54,7 @@ inline VirtualAddress operator-(const VirtualAddress& a, const VirtualAddress& b
template<>
struct AK::Formatter<VirtualAddress> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, const VirtualAddress& value)
ErrorOr<void> format(FormatBuilder& builder, const VirtualAddress& value)
{
return AK::Formatter<FormatString>::format(builder, "V{}", value.as_ptr());
}