aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp
diff options
context:
space:
mode:
authordgolear <dgolear@yandex-team.com>2025-03-06 20:14:19 +0300
committerdgolear <dgolear@yandex-team.com>2025-03-06 21:05:52 +0300
commit115c3a54ab9250023ac3db71c4353b53ea5fd30e (patch)
treeff3e4e9358f3b55dde74ea6a4c1047f0b9d6c5ab /library/cpp
parentd24c0a7b912871b026df923c1a685bfff950b1f7 (diff)
downloadydb-115c3a54ab9250023ac3db71c4353b53ea5fd30e.tar.gz
YT: Allow serializing and deserializing plain enums to uint64
commit_hash:abf11126ef1a914939d506a79dd7c4f11df177f2
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/yt/misc/cast-inl.h8
-rw-r--r--library/cpp/yt/misc/unittests/cast_ut.cpp21
2 files changed, 24 insertions, 5 deletions
diff --git a/library/cpp/yt/misc/cast-inl.h b/library/cpp/yt/misc/cast-inl.h
index 5e5a0d90eea..e86f420431b 100644
--- a/library/cpp/yt/misc/cast-inl.h
+++ b/library/cpp/yt/misc/cast-inl.h
@@ -83,20 +83,20 @@ inline TString FormatInvalidCastValue(char8_t value)
template <class T, class S>
constexpr bool CanFitSubtype()
{
- return NDetail::IsInIntegralRange<T>(std::numeric_limits<S>::min()) &&
- NDetail::IsInIntegralRange<T>(std::numeric_limits<S>::max());
+ return NYT::NDetail::IsInIntegralRange<T>(std::numeric_limits<S>::min()) &&
+ NYT::NDetail::IsInIntegralRange<T>(std::numeric_limits<S>::max());
}
template <class T, class S>
constexpr bool IsInIntegralRange(S value)
{
- return NDetail::IsInIntegralRange<T>(value);
+ return NYT::NDetail::IsInIntegralRange<T>(value);
}
template <class T, class S>
constexpr std::optional<T> TryCheckedIntegralCast(S value)
{
- [[unlikely]] if (!NDetail::IsInIntegralRange<T>(value)) {
+ [[unlikely]] if (!NYT::NDetail::IsInIntegralRange<T>(value)) {
return std::nullopt;
}
return static_cast<T>(value);
diff --git a/library/cpp/yt/misc/unittests/cast_ut.cpp b/library/cpp/yt/misc/unittests/cast_ut.cpp
index e6f5409f2ff..ebb83485f89 100644
--- a/library/cpp/yt/misc/unittests/cast_ut.cpp
+++ b/library/cpp/yt/misc/unittests/cast_ut.cpp
@@ -87,8 +87,27 @@ TEST(TCastTest, CheckedEnumCast)
EXPECT_EQ(CheckedEnumCast<ELangsWithUnknown>(0x41), ELangsWithUnknown::Unknown | ELangsWithUnknown::Cpp);
}
+TEST(TCastTest, IntegralCasts)
+{
+ static_assert(CanFitSubtype<i64, i32>());
+ static_assert(CanFitSubtype<ui64, ui32>());
+ static_assert(CanFitSubtype<ui64, ui64>());
+ static_assert(!CanFitSubtype<ui64, i32>());
+ static_assert(!CanFitSubtype<i32, i64>());
+
+ static_assert(IsInIntegralRange<ui32>(0));
+ static_assert(IsInIntegralRange<ui32>(1ull));
+ static_assert(!IsInIntegralRange<ui32>(-1));
+ static_assert(!IsInIntegralRange<i32>(std::numeric_limits<i64>::max()));
+
+ static_assert(IsInIntegralRange<i32>(1ull));
+ static_assert(IsInIntegralRange<i32>(-1));
+ static_assert(!IsInIntegralRange<ui32>(-1));
+ static_assert(!IsInIntegralRange<ui32>(std::numeric_limits<i64>::max()));
+ static_assert(IsInIntegralRange<ui64>(std::numeric_limits<i64>::max()));
+}
+
////////////////////////////////////////////////////////////////////////////////
} // namespace
} // namespace NYT
-