Skip to content

Commit

Permalink
sstring: deprecate formatters for vector and unordered_map
Browse files Browse the repository at this point in the history
Seastar is an event-driven framework, not a collection of libraries
for multiple purposes. also, when migrating to a {fmt} only formatting
solution, the operator<< based printers can actually makes our lives
more difficult. for instance, Boost.test expects operator<< and fall
back to `boost_test_print_type()`, so even if we provide a templated
`boost_test_print_type()` which accepts all types which can be formatted
with {fmt}, if Boost.test is able to find the operator<<-based
formatter for a std::vector, it just picks it, and then hits the brick
wall, as the elements in the vector does not provide an operator<<-based
formatter.

instead of enabling these operator<<:s to print the element with
fmt::formatter support, let's just disable them on user's request.

in this change, a new option is added. so that user can disable these
formatter on request. and these two formatters are marked deprecated.
so in future, we can remove them when we bump up the API level or just
completely drop them.

Fixes scylladb#1544
Signed-off-by: Kefu Chai <[email protected]>
  • Loading branch information
tchaikov committed May 21, 2024
1 parent 67a14f9 commit 3e2977b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ option (Seastar_SSTRING
"Use seastar's own string implementation"
ON)

option (Seastar_DEPRECATED_OSTREAM_FORMATTERS
"Enable operator<< for formatting standard library containers, which will be deprecated in future"
ON)

set (Seastar_API_LEVEL
"7"
CACHE
Expand Down Expand Up @@ -927,6 +931,11 @@ if (Seastar_SSTRING)
PUBLIC SEASTAR_SSTRING)
endif ()

if (Seastar_DEPRECATED_OSTREAM_FORMATTERS)
target_compile_definitions (seastar
PUBLIC SEASTAR_DEPRECATED_OSTREAM_FORMATTERS)
endif ()

if (LinuxMembarrier_FOUND)
list (APPEND Seastar_PRIVATE_COMPILE_DEFINITIONS SEASTAR_HAS_MEMBARRIER)

Expand Down
6 changes: 6 additions & 0 deletions include/seastar/core/sstring.hh
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,13 @@ string_type to_sstring(T value) {
}
}

#ifdef SEASTAR_DEPRECATED_OSTREAM_FORMATTERS

namespace std {

SEASTAR_MODULE_EXPORT
template <typename T>
[[deprecated("Use {fmt} instead")]]
inline
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
bool first = true;
Expand All @@ -858,6 +861,7 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {

SEASTAR_MODULE_EXPORT
template <typename Key, typename T, typename Hash, typename KeyEqual, typename Allocator>
[[deprecated("Use {fmt} instead")]]
std::ostream& operator<<(std::ostream& os, const std::unordered_map<Key, T, Hash, KeyEqual, Allocator>& v) {
bool first = true;
os << "{";
Expand All @@ -874,6 +878,8 @@ std::ostream& operator<<(std::ostream& os, const std::unordered_map<Key, T, Hash
}
}

#endif

#if FMT_VERSION >= 90000

// Due to https://github.com/llvm/llvm-project/issues/68849, we inherit
Expand Down

0 comments on commit 3e2977b

Please sign in to comment.