Kernel: Specialize Traits<(Nonnull)OwnPtr<T>> for KString

To make it behave like a string, since KString is always stored as a
(Nonnull)OwnPtr in the kernel.
This commit is contained in:
Ali Mohammad Pur 2021-09-12 00:15:47 +04:30 committed by Ali Mohammad Pur
parent a21a3c2620
commit d600f0d5b3
Notes: sideshowbarker 2024-07-18 04:03:46 +09:00

View File

@ -63,4 +63,33 @@ struct Formatter<OwnPtr<Kernel::KString>> : Formatter<StringView> {
}
};
template<>
struct Traits<NonnullOwnPtr<Kernel::KString>> : public GenericTraits<NonnullOwnPtr<Kernel::KString>> {
using PeekType = Kernel::KString*;
using ConstPeekType = Kernel::KString const*;
static unsigned hash(NonnullOwnPtr<Kernel::KString> const& p) { return string_hash(p->characters(), p->length()); }
static bool equals(NonnullOwnPtr<Kernel::KString> const& a, NonnullOwnPtr<Kernel::KString> const& b) { return a->view() == b->view(); }
};
template<>
struct Traits<OwnPtr<Kernel::KString>> : public GenericTraits<OwnPtr<Kernel::KString>> {
using PeekType = Kernel::KString*;
using ConstPeekType = Kernel::KString const*;
static unsigned hash(OwnPtr<Kernel::KString> const& p)
{
if (!p)
return ptr_hash(nullptr);
return string_hash(p->characters(), p->length());
}
static bool equals(OwnPtr<Kernel::KString> const& a, OwnPtr<Kernel::KString> const& b)
{
if (!a || !b)
return a.ptr() == b.ptr();
if (a == b)
return true;
return a->view() == b->view();
}
};
}