Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Implement a universal printer #51151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/ray/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ ray_cc_library(
ray_cc_library(
name = "type_traits",
hdrs = ["type_traits.h"],
deps = [
"@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/strings:str_format",
],
)

ray_cc_library(
Expand Down Expand Up @@ -373,3 +377,11 @@ ray_cc_library(
":logging",
],
)

ray_cc_library(
name = "visitor",
hdrs = ["visitor.h"],
deps = [
"@com_google_absl//absl/meta:type_traits",
],
)
156 changes: 156 additions & 0 deletions src/ray/util/strings/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
load("//bazel:ray.bzl", "ray_cc_library", "ray_cc_test")

package(default_visibility = ["//visibility:public"])

ray_cc_library(
name = "type_printer",
hdrs = ["type_printer.h"],
deps = [
":get_type_name",
"@boost//:core",
],
)

ray_cc_library(
name = "std_printer",
hdrs = ["std_printer.h"],
deps = [
":ostream_printer",
],
)

ray_cc_test(
name = "std_printer_test",
srcs = ["std_printer_test.cc"],
tags = ["team:core"],
deps = [
":std_printer",
"@com_google_absl//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "debug_string_printer",
hdrs = ["debug_string_printer.h"],
)

ray_cc_test(
name = "debug_string_printer_test",
srcs = ["debug_string_printer_test.cc"],
tags = ["team:core"],
deps = [
":debug_string_printer",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "absl_stringify_printer",
hdrs = ["absl_stringify_printer.h"],
deps = [
"@com_google_absl//absl/strings",
],
)

ray_cc_test(
name = "absl_stringify_printer_test",
srcs = ["absl_stringify_printer_test.cc"],
tags = ["team:core"],
deps = [
":absl_stringify_printer",
"@com_google_absl//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "container_printer",
hdrs = ["container_printer.h"],
deps = [
":ostream_printer",
"//src/ray/util:type_traits",
],
)

ray_cc_test(
name = "container_printer_test",
srcs = ["container_printer_test.cc"],
tags = ["team:core"],
deps = [
":container_printer",
"//src/ray/util:type_traits",
"//src/ray/util:visitor",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "ostream_printer",
hdrs = ["ostream_printer.h"],
)

ray_cc_test(
name = "ostream_printer_test",
srcs = ["ostream_printer_test.cc"],
tags = ["team:core"],
deps = [
":ostream_printer",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "enum_printer",
hdrs = ["enum_printer.h"],
)

ray_cc_test(
name = "enum_printer_test",
srcs = ["enum_printer_test.cc"],
tags = ["team:core"],
deps = [
":enum_printer",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "debug_printer",
hdrs = ["debug_printer.h"],
deps = [
":absl_stringify_printer",
":container_printer",
":debug_string_printer",
":enum_printer",
":ostream_printer",
":std_printer",
":type_printer",
"//src/ray/util:visitor",
],
)

ray_cc_test(
name = "debug_printer_test",
srcs = ["debug_printer_test.cc"],
tags = ["team:core"],
deps = [
":debug_printer",
"@com_google_googletest//:gtest_main",
],
)

ray_cc_library(
name = "get_type_name",
hdrs = ["get_type_name.h"],
)

ray_cc_test(
name = "get_type_name_test",
srcs = ["get_type_name_test.cc"],
tags = ["team:core"],
deps = [
":get_type_name",
"@com_google_googletest//:gtest_main",
],
)
47 changes: 47 additions & 0 deletions src/ray/util/strings/absl_stringify_printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2025 The Ray Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Printer for types which defines `AbslStringify`.

#pragma once

#include <iostream>
#include <type_traits>

#include "absl/strings/str_cat.h"

namespace ray {

// TODO(hjiang): Later version of abseil contains abseil string defined trait.
template <typename T, typename = void, typename = void>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are the two extra void template params for

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the fallback template argument (basically for SFINAE, fallback template type should match L31-L34)

struct is_stringifyable : std::false_type {};

template <typename T, typename SinkType>
struct is_stringifyable<T,
SinkType,
std::void_t<decltype(AbslStringify(std::declval<SinkType &>(),
std::declval<const T &>()))>>
: std::true_type {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you write this differently with concepts, that should make this easier to read? (in perfect cpp20 world)

Copy link
Contributor Author

@dentiny dentiny Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah in my prev company all in concepts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And yes, if we do have C++20 I will update to concepts and modern type traits


template <typename T>
inline constexpr bool is_stringifyable_v = is_stringifyable<T, std::ostream>::value;

struct AbslStringifyPrinter {
template <typename T, std::enable_if_t<is_stringifyable_v<T>, int> = 0>
void operator()(std::ostream &os, const T &obj) const {
os << absl::StrCat(obj);
}
};

} // namespace ray
47 changes: 47 additions & 0 deletions src/ray/util/strings/absl_stringify_printer_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2025 The Ray Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "ray/util/strings/absl_stringify_printer.h"

#include <gtest/gtest.h>

#include "absl/strings/str_format.h"

namespace ray {

namespace {

struct Point {
template <typename Sink>
friend void AbslStringify(Sink &sink, const Point &p) {
absl::Format(&sink, "(%d, %d)", p.x, p.y);
}

int x;
int y;
};

TEST(AbslStringifyTest, BasicTest) {
Point point;
point.x = 10;
point.y = 20;

std::stringstream ss;
AbslStringifyPrinter{}(ss, point);
EXPECT_EQ(ss.str(), "(10, 20)");
}

} // namespace

} // namespace ray
Loading