add a fmt formatter for Hash20

Summary: We're migrating to fmt, so add a formatter for Hash20.

Reviewed By: xavierd

Differential Revision: D41104916

fbshipit-source-id: 51ecbd61f1b5ba2fe257cd6ef5835bc9030b1a0a
This commit is contained in:
Chad Austin 2022-11-11 18:46:15 -08:00 committed by Facebook GitHub Bot
parent 36cf1e188e
commit f109c56273
3 changed files with 16 additions and 3 deletions

View File

@ -156,5 +156,13 @@ struct hash<facebook::eden::Hash20> {
return hash.getHashCode();
}
};
} // namespace std
template <>
struct fmt::formatter<facebook::eden::Hash20> : formatter<string_view> {
template <typename Context>
auto format(const facebook::eden::Hash20& h, Context& ctx) const {
// TODO: Avoid allocation here.
return formatter<string_view>::format(h.toString(), ctx);
}
};

View File

@ -219,3 +219,10 @@ TEST(Hash20, getHashCode) {
// using 64 bits of data to contribute to the hash code.
EXPECT_EQ(folly::Endian::big(0xfaceb00cdeadbeef), testHash.getHashCode());
}
TEST(Hash20, formatting) {
Hash20 h("0123456789abcdeffedcba987654321076543210");
EXPECT_EQ(
"0123456789abcdeffedcba987654321076543210", folly::to<std::string>(h));
EXPECT_EQ("0123456789abcdeffedcba987654321076543210", fmt::to_string(h));
}

View File

@ -22,9 +22,7 @@
#include <folly/lang/Bits.h>
#include <folly/logging/xlog.h>
#include <folly/portability/GFlags.h>
#ifndef _WIN32
#include <folly/portability/Unistd.h>
#endif
#include <mutex>