From 50964c423efad872927fd65e69dbdf8ad0693493 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 15 Feb 2024 10:51:46 +0800 Subject: [PATCH] hints: host_filter: add formatter for hints::host_filter before this change, we rely on the default-generated fmt::formatter created from operator<<, but fmt v10 dropped the default-generated formatter. in this change, we define formatters for `hints::host_filter`. its operator<< is preserved as it's still used by the homebrew generic formatter for vector<>, which is in turn used by db/config.cc. Refs #13245 Signed-off-by: Kefu Chai Closes scylladb/scylladb#17347 --- db/hints/host_filter.cc | 24 +++++++++++++++--------- db/hints/host_filter.hh | 8 +++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/db/hints/host_filter.cc b/db/hints/host_filter.cc index 8a02411721e9..1795addb9736 100644 --- a/db/hints/host_filter.cc +++ b/db/hints/host_filter.cc @@ -68,6 +68,11 @@ host_filter host_filter::parse_from_dc_list(sstring opt) { return host_filter(std::unordered_set(dcs.begin(), dcs.end())); } +std::ostream& operator<<(std::ostream& os, const host_filter& f) { + fmt::print(os, "{}", f); + return os; +} + std::istream& operator>>(std::istream& is, host_filter& f) { sstring tmp; is >> tmp; @@ -100,16 +105,17 @@ std::string_view host_filter::enabled_kind_to_string(host_filter::enabled_kind e throw std::logic_error("Uncovered variant of enabled_kind"); } -std::ostream& operator<<(std::ostream& os, const host_filter& f) { - fmt::print(os, "host_filter{{enabled_kind={}", +} +} + +auto fmt::formatter::format(const db::hints::host_filter& f, fmt::format_context& ctx) const + -> decltype(ctx.out()) { + using db::hints::host_filter; + auto out = ctx.out(); + out = fmt::format_to(out, "host_filter{{enabled_kind={}", host_filter::enabled_kind_to_string(f._enabled_kind)); if (f._enabled_kind == host_filter::enabled_kind::enabled_selectively) { - fmt::print(os, ", dcs={{{}}}", fmt::join(f._dcs, ",")); + out = fmt::format_to(out, ", dcs={{{}}}", fmt::join(f._dcs, ",")); } - fmt::print(os, "}}"); - return os; -} - -} + return fmt::format_to(out, "}}"); } - diff --git a/db/hints/host_filter.hh b/db/hints/host_filter.hh index 06791e9b2279..3b1fed9ad391 100644 --- a/db/hints/host_filter.hh +++ b/db/hints/host_filter.hh @@ -79,9 +79,10 @@ public: sstring to_configuration_string() const; - friend std::ostream& operator<<(std::ostream& os, const host_filter& f); + friend fmt::formatter; }; +std::ostream& operator<<(std::ostream& os, const host_filter& f); std::istream& operator>>(std::istream& is, host_filter& f); class hints_configuration_parse_error : public std::runtime_error { @@ -91,3 +92,8 @@ public: } } + +template <> +struct fmt::formatter : fmt::formatter { + auto format(const db::hints::host_filter&, fmt::format_context& ctx) const -> decltype(ctx.out()); +};