utils/managed_bytes: add fmt::formatters for managed_bytes and friends

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

* managed_bytes
* managed_bytes_view
* managed_bytes_opt

Refs #13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
This commit is contained in:
Kefu Chai
2024-02-22 11:50:04 +08:00
parent 3d9054991b
commit 3835ebfcdc
3 changed files with 31 additions and 16 deletions

View File

@@ -153,6 +153,7 @@ public:
return {typename R<clustering_key_prefix_view>::bound(bv._prefix.get().view(), inclusive)};
}
friend std::ostream& operator<<(std::ostream& out, const bound_view& b) {
return out << "{bound: prefix=" << b._prefix.get() << ", kind=" << b._kind << "}";
fmt::print(out, "{{bound: prefix={}, kind={}}}", b._prefix.get(), b._kind);
return out;
}
};

View File

@@ -28,16 +28,9 @@ managed_bytes_opt to_managed_bytes_opt(const bytes_opt& bo) {
}
sstring to_hex(const managed_bytes& b) {
return fmt::to_string(managed_bytes_view(b));
return seastar::format("{}", managed_bytes_view(b));
}
sstring to_hex(const managed_bytes_opt& b) {
return !b ? "null" : to_hex(*b);
}
std::ostream& operator<<(std::ostream& os, const managed_bytes_opt& b) {
if (b) {
return os << *b;
}
return os << "null";
}

View File

@@ -591,15 +591,36 @@ struct hash<managed_bytes> {
sstring to_hex(const managed_bytes& b);
sstring to_hex(const managed_bytes_opt& b);
// The operators below are used only by tests.
inline std::ostream& operator<<(std::ostream& os, const managed_bytes_view& v) {
for (bytes_view frag : fragment_range(v)) {
os << to_hex(frag);
// The formatters below are used only by tests.
template <> struct fmt::formatter<managed_bytes_view> : fmt::formatter<std::string_view> {
auto format(const managed_bytes_view& v, fmt::format_context& ctx) const {
auto out = ctx.out();
for (bytes_view frag : fragment_range(v)) {
out = fmt::format_to(out, "{}", fmt_hex(frag));
}
return out;
}
};
inline std::ostream& operator<<(std::ostream& os, const managed_bytes_view& v) {
fmt::print(os, "{}", v);
return os;
}
template <> struct fmt::formatter<managed_bytes> : fmt::formatter<std::string_view> {
auto format(const managed_bytes& b, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "{}", managed_bytes_view(b));
}
};
inline std::ostream& operator<<(std::ostream& os, const managed_bytes& b) {
return (os << managed_bytes_view(b));
fmt::print(os, "{}", b);
return os;
}
std::ostream& operator<<(std::ostream& os, const managed_bytes_opt& b);
template <> struct fmt::formatter<managed_bytes_opt> : fmt::formatter<std::string_view> {
auto format(const managed_bytes_opt& opt, fmt::format_context& ctx) const {
if (opt) {
return fmt::format_to(ctx.out(), "{}", *opt);
}
return fmt::format_to(ctx.out(), "null");
}
};