mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
AK: Make Formatter for NonnullOwnPtr<T> format the T
This mirrors the behavior of NonnullRefPtr<T>. If you want to format the pointer address, call .ptr() on it.
This commit is contained in:
parent
f8e5df7a99
commit
b256e52586
2 changed files with 26 additions and 0 deletions
|
@ -196,7 +196,16 @@ inline void swap(NonnullOwnPtr<T>& a, NonnullOwnPtr<U>& b)
|
||||||
a.swap(b);
|
a.swap(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<Formattable T>
|
||||||
|
struct Formatter<NonnullOwnPtr<T>> : Formatter<T> {
|
||||||
|
ErrorOr<void> format(FormatBuilder& builder, NonnullOwnPtr<T> const& value)
|
||||||
|
{
|
||||||
|
return Formatter<T>::format(builder, *value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
requires(!HasFormatter<T>)
|
||||||
struct Formatter<NonnullOwnPtr<T>> : Formatter<T const*> {
|
struct Formatter<NonnullOwnPtr<T>> : Formatter<T const*> {
|
||||||
ErrorOr<void> format(FormatBuilder& builder, NonnullOwnPtr<T> const& value)
|
ErrorOr<void> format(FormatBuilder& builder, NonnullOwnPtr<T> const& value)
|
||||||
{
|
{
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/NonnullOwnPtr.h>
|
#include <AK/NonnullOwnPtr.h>
|
||||||
#include <AK/OwnPtr.h>
|
#include <AK/OwnPtr.h>
|
||||||
|
#include <AK/String.h>
|
||||||
|
|
||||||
TEST_CASE(destroy_self_owning_object)
|
TEST_CASE(destroy_self_owning_object)
|
||||||
{
|
{
|
||||||
|
@ -32,3 +33,19 @@ TEST_CASE(destroy_self_owning_object)
|
||||||
object_ptr->inner = make<SelfOwning::Inner>(object.release_nonnull());
|
object_ptr->inner = make<SelfOwning::Inner>(object.release_nonnull());
|
||||||
object_ptr->inner = nullptr;
|
object_ptr->inner = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Foo { };
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AK::Formatter<Foo> : Formatter<StringView> {
|
||||||
|
ErrorOr<void> format(FormatBuilder& builder, Foo const&)
|
||||||
|
{
|
||||||
|
return Formatter<StringView>::format(builder, ":^)"sv);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_CASE(formatter)
|
||||||
|
{
|
||||||
|
auto foo = make<Foo>();
|
||||||
|
EXPECT_EQ(MUST(String::formatted("{}", foo)), ":^)"sv);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue