dht: specialize fmt::formatter<dht::token>

this is a part of a series to migrating from `operator<<(ostream&, ..)`
based formatting to fmtlib based formatting. the goal here is to enable
fmtlib to print `dht::token` without the help of `operator<<`.

the corresponding `operator<<()` is preserved in this change, as it
has lots of users in this project, we will tackle them case-by-case in
follow-up changes.

Refs #13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
This commit is contained in:
Kefu Chai
2023-04-20 18:52:23 +08:00
parent ad065aaa62
commit 53dedca8cd
2 changed files with 15 additions and 7 deletions

View File

@@ -52,13 +52,7 @@ std::strong_ordering tri_compare(const token& t1, const token& t2) {
}
std::ostream& operator<<(std::ostream& out, const token& t) {
if (t._kind == token::kind::after_all_keys) {
out << "maximum token";
} else if (t._kind == token::kind::before_all_keys) {
out << "minimum token";
} else {
out << t.to_sstring();
}
fmt::print(out, "{}", t);
return out;
}

View File

@@ -223,3 +223,17 @@ token bias(uint64_t n);
size_t compaction_group_of(unsigned most_significant_bits, const token& t);
} // namespace dht
template <>
struct fmt::formatter<dht::token> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const dht::token& t, FormatContext& ctx) const {
if (t.is_maximum()) {
return fmt::format_to(ctx.out(), "maximum token");
} else if (t.is_minimum()) {
return fmt::format_to(ctx.out(), "minimum token");
} else {
return fmt::format_to(ctx.out(), "{}", dht::token::to_int64(t));
}
}
};