utils/human_readable: add fmt::formatter for human_readable_value

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 `utils::human_readable_value`,
and drop its operator<<

Refs #13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
This commit is contained in:
Kefu Chai
2024-03-09 21:08:12 +08:00
parent 007d7f1355
commit 9300d7b80b
2 changed files with 15 additions and 12 deletions

View File

@@ -13,14 +13,6 @@
namespace utils {
std::ostream& operator<<(std::ostream& os, const human_readable_value& val) {
os << val.value;
if (val.suffix) {
os << val.suffix;
}
return os;
}
static human_readable_value to_human_readable_value(uint64_t value, uint64_t step, uint64_t precision, const std::array<char, 5>& suffixes) {
if (!value) {
return {0, suffixes[0]};
@@ -48,3 +40,12 @@ human_readable_value to_hr_size(uint64_t size) {
}
} // namespace utils
auto fmt::formatter<utils::human_readable_value>::format(const utils::human_readable_value& val, fmt::format_context& ctx) const
-> decltype(ctx.out()) {
auto out = fmt::format_to(ctx.out(), "{}", val.value);
if (val.suffix) {
out = fmt::format_to(out, "{}", val.suffix);
}
return out;
}

View File

@@ -8,8 +8,8 @@
#pragma once
#include <cinttypes>
#include <iosfwd>
#include <cstdint>
#include <fmt/core.h>
namespace utils {
@@ -18,8 +18,6 @@ struct human_readable_value {
char suffix; // 0 -> no suffix
};
std::ostream& operator<<(std::ostream& os, const human_readable_value& val);
/// Convert a size to a human readable representation.
///
/// The human-readable representation has at most 4 digits
@@ -38,3 +36,7 @@ std::ostream& operator<<(std::ostream& os, const human_readable_value& val);
human_readable_value to_hr_size(uint64_t size);
} // namespace utils
template <> struct fmt::formatter<utils::human_readable_value> : fmt::formatter<std::string_view> {
auto format(const utils::human_readable_value&, fmt::format_context& ctx) const -> decltype(ctx.out());
};