summaryrefslogtreecommitdiffstats
path: root/library/cpp
diff options
context:
space:
mode:
authornechda <[email protected]>2024-08-06 10:15:22 +0300
committernechda <[email protected]>2024-08-06 10:26:34 +0300
commite842851d5b299675ac1be291a152853c339a9add (patch)
tree32597289dcf103c49f524c6411801bc500261d6c /library/cpp
parentec2f22a4794efe938e85da54c86f06e19b3173dc (diff)
Dont generate ctors for TRange & TMutableRange if protobuf cant handle it
9aa0e068d05932c7ea3065408ed3dbb7fddb6321
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/yt/memory/range.h15
-rw-r--r--library/cpp/yt/memory/unittests/range_protobuf_repeated_field_ut.cpp76
-rw-r--r--library/cpp/yt/memory/unittests/ya.make1
3 files changed, 90 insertions, 2 deletions
diff --git a/library/cpp/yt/memory/range.h b/library/cpp/yt/memory/range.h
index 932f7d93c2d..420ee9b4294 100644
--- a/library/cpp/yt/memory/range.h
+++ b/library/cpp/yt/memory/range.h
@@ -9,6 +9,7 @@
#include <optional>
#include <span>
#include <vector>
+#include <type_traits>
// For size_t.
#include <stddef.h>
@@ -60,6 +61,12 @@ public:
using const_iterator = const T*;
using size_type = size_t;
+ static constexpr bool CAnyRepeatedField = requires {
+ { google::protobuf::RepeatedField<T>{} }
+ ->
+ std::same_as<google::protobuf::RepeatedField<T>>;
+ };
+
//! Constructs a null TRange.
TRange()
: Data_(nullptr)
@@ -126,7 +133,9 @@ public:
{ }
//! Constructs a TRange from RepeatedField.
- TRange(const google::protobuf::RepeatedField<T>& elements)
+ template<std::same_as<T> U = T>
+ requires CAnyRepeatedField
+ TRange(const google::protobuf::RepeatedField<U>& elements)
: Data_(elements.data())
, Length_(elements.size())
{ }
@@ -333,7 +342,9 @@ public:
{ }
//! Constructs a TMutableRange from RepeatedField.
- TMutableRange(google::protobuf::RepeatedField<T>& elements)
+ template<std::same_as<T> U = T>
+ requires TRange<T>::CAnyRepeatedField
+ TMutableRange(google::protobuf::RepeatedField<U>& elements)
: TRange<T>(elements)
{ }
diff --git a/library/cpp/yt/memory/unittests/range_protobuf_repeated_field_ut.cpp b/library/cpp/yt/memory/unittests/range_protobuf_repeated_field_ut.cpp
new file mode 100644
index 00000000000..35581468a4e
--- /dev/null
+++ b/library/cpp/yt/memory/unittests/range_protobuf_repeated_field_ut.cpp
@@ -0,0 +1,76 @@
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <library/cpp/testing/gtest_extensions/assertions.h>
+
+#include <library/cpp/yt/memory/range.h>
+#include <vector>
+
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+/*
+ Test build
+ https://a.yandex-team.ru/arcadia/yt/yt/library/backtrace_introspector/introspect.cpp?rev=r14560536#L165
+*/
+
+TEST(TRangeVector, ImmutableRange)
+{
+ std::vector<const void*> backtrace;
+ [[maybe_unused]] auto range = NYT::TRange(backtrace);
+}
+
+TEST(TRangeVector, MutableRange)
+{
+ std::vector<const void*> backtrace;
+ [[maybe_unused]] auto range = NYT::TMutableRange(backtrace);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/*
+ Test build
+ https://a.yandex-team.ru/arcadia/yt/yt/library/oom/oom.cpp?rev=r14560536#L112
+*/
+
+static const char* TDummyStringArray[] = {
+ "xxx",
+ "yyy",
+ "zzz",
+};
+
+TEST(TRangeArrayOfStrings, ImmutableRange)
+{
+ [[maybe_unused]] auto range = NYT::TRange(TDummyStringArray);
+}
+
+TEST(TRangeArrayOfStrings, MutableRange)
+{
+ [[maybe_unused]] auto range = NYT::TMutableRange(TDummyStringArray);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/*
+ Test build
+ https://a.yandex-team.ru/arcadia/yt/yt/orm/server/access_control/object_cluster.cpp?rev=r14560536#L188
+*/
+
+TEST(TRangeRawPtr, ImmutableRange)
+{
+ struct TDummyData{};
+ TDummyData** ptr = nullptr;
+ constexpr int size = 0x10;
+ [[maybe_unused]] auto range = NYT::TRange<TDummyData*>(ptr, size);
+}
+
+TEST(TRangeRawPtr, MutableRange)
+{
+ struct TDummyData{};
+ TDummyData** ptr = nullptr;
+ constexpr int size = 0x10;
+ [[maybe_unused]] auto range = NYT::TMutableRange<TDummyData*>(ptr, size);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+} // namespace
diff --git a/library/cpp/yt/memory/unittests/ya.make b/library/cpp/yt/memory/unittests/ya.make
index d2f098fa722..920873de265 100644
--- a/library/cpp/yt/memory/unittests/ya.make
+++ b/library/cpp/yt/memory/unittests/ya.make
@@ -18,6 +18,7 @@ SRCS(
shared_range_ut.cpp
weak_ptr_ut.cpp
ref_ut.cpp
+ range_protobuf_repeated_field_ut.cpp
)
IF (NOT OS_WINDOWS)