aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp
diff options
context:
space:
mode:
authorarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-12-25 10:19:49 +0300
committerarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-12-25 11:15:51 +0300
commit47a8704130b3d47dcb4eb806c3eae41292e90eb4 (patch)
tree2071f3873a5a8063affe776e4b83866ab997b1a2 /library/cpp
parent2f83984f3c96794039f6d577c2dc3eea220dbca0 (diff)
downloadydb-47a8704130b3d47dcb4eb806c3eae41292e90eb4.tar.gz
YT-21233: Issues and tidying up of rXXXXXX
commit_hash:19481c9fbb008aab4f4d676f1a3a242f6e90e90e
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/yt/error/error-inl.h11
-rw-r--r--library/cpp/yt/error/error.cpp8
-rw-r--r--library/cpp/yt/error/error.h3
-rw-r--r--library/cpp/yt/error/error_attributes-inl.h18
-rw-r--r--library/cpp/yt/error/mergeable_dictionary.h55
-rw-r--r--library/cpp/yt/string/string.cpp3
6 files changed, 39 insertions, 59 deletions
diff --git a/library/cpp/yt/error/error-inl.h b/library/cpp/yt/error/error-inl.h
index f9a7355d1a9..c1ba73fbcf0 100644
--- a/library/cpp/yt/error/error-inl.h
+++ b/library/cpp/yt/error/error-inl.h
@@ -154,17 +154,6 @@ TError TError::Wrap(TErrorCode code, TFormatString<TArgs...> format, TArgs&&...
#undef IMPLEMENT_COPY_WRAP
#undef IMPLEMENT_MOVE_WRAP
-template <CMergeableDictionary TDictionary>
-TError& TError::operator <<= (const TDictionary& attributes) &
-{
- // This forces inclusion of error_attributes in the header file
- // which is undesirable.
- // One could (and probably should) implement type-erasure
- // like AnyDictionaryRef to move this implementation in cpp file.
- MutableAttributes()->MergeFrom(attributes);
- return *this;
-}
-
template <CErrorNestable TValue>
TError&& TError::operator << (TValue&& rhs) &&
{
diff --git a/library/cpp/yt/error/error.cpp b/library/cpp/yt/error/error.cpp
index bcc265ab2ed..59eea6ce8c6 100644
--- a/library/cpp/yt/error/error.cpp
+++ b/library/cpp/yt/error/error.cpp
@@ -435,7 +435,7 @@ bool TError::HasAttributes() const noexcept
const TErrorAttributes& TError::Attributes() const
{
if (!Impl_) {
- static TErrorAttributes empty = {};
+ static TErrorAttributes empty;
return empty;
}
return Impl_->Attributes();
@@ -688,6 +688,12 @@ TError& TError::operator <<= (std::vector<TError>&& innerErrors) &
return *this;
}
+TError& TError::operator <<= (TAnyMergeableDictionaryRef attributes) &
+{
+ MutableAttributes()->MergeFrom(attributes);
+ return *this;
+}
+
////////////////////////////////////////////////////////////////////////////////
bool operator == (const TError& lhs, const TError& rhs)
diff --git a/library/cpp/yt/error/error.h b/library/cpp/yt/error/error.h
index 6412ad029b8..eed1cfb75d5 100644
--- a/library/cpp/yt/error/error.h
+++ b/library/cpp/yt/error/error.h
@@ -202,8 +202,7 @@ public:
TError& operator <<= (TError&& innerError) &;
TError& operator <<= (const std::vector<TError>& innerErrors) &;
TError& operator <<= (std::vector<TError>&& innerErrors) &;
- template <CMergeableDictionary TDictionary>
- TError& operator <<= (const TDictionary& attributes) &;
+ TError& operator <<= (TAnyMergeableDictionaryRef attributes) &;
template <CErrorNestable TValue>
TError&& operator << (TValue&& operand) &&;
diff --git a/library/cpp/yt/error/error_attributes-inl.h b/library/cpp/yt/error/error_attributes-inl.h
index 0a77d9afb0c..e6b39f531e9 100644
--- a/library/cpp/yt/error/error_attributes-inl.h
+++ b/library/cpp/yt/error/error_attributes-inl.h
@@ -78,23 +78,21 @@ typename TOptionalTraits<T>::TOptional TErrorAttributes::FindAndRemove(const TKe
template <CMergeableDictionary TDictionary>
void TErrorAttributes::MergeFrom(const TDictionary& dict)
{
- using TTraits = TMergeDictionariesTraits<TDictionary>;
-
- for (const auto& [key, value] : TTraits::MakeIterableView(dict)) {
+ for (auto range = AsMergeableRange(dict); const auto& [key, value] : range) {
SetValue(key, value);
}
}
////////////////////////////////////////////////////////////////////////////////
-template <>
-struct TMergeDictionariesTraits<TErrorAttributes>
+namespace NMergeableRangeImpl {
+
+inline TMergeableRange TagInvoke(TTagInvokeTag<AsMergeableRange>, const TErrorAttributes& attributes)
{
- static auto MakeIterableView(const TErrorAttributes& attributes)
- {
- return attributes.ListPairs();
- }
-};
+ return attributes.ListPairs();
+}
+
+} // namespace NMergeableRangeImpl
static_assert(CMergeableDictionary<TErrorAttributes>);
diff --git a/library/cpp/yt/error/mergeable_dictionary.h b/library/cpp/yt/error/mergeable_dictionary.h
index 361694d8416..cf866f3da7c 100644
--- a/library/cpp/yt/error/mergeable_dictionary.h
+++ b/library/cpp/yt/error/mergeable_dictionary.h
@@ -1,58 +1,45 @@
#pragma once
-#include "public.h"
#include "error_attribute.h"
+#include <library/cpp/yt/memory/type_erasure.h>
+
+#include <library/cpp/yt/misc/tag_invoke_cpo.h>
+
#include <ranges>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
-// Can be specialized to make your dictionary satisfy CMergeableDictionary.
-template <class TDictionary>
-struct TMergeDictionariesTraits
-{
- static auto MakeIterableView(const TDictionary& dict)
- requires false;
-};
+namespace NMergeableRangeImpl {
+
+struct TFn
+ : public TTagInvokeCpoBase<TFn>
+{ };
////////////////////////////////////////////////////////////////////////////////
-namespace NDetail {
+using TMergeableRange = std::vector<std::pair<TErrorAttribute::TKey, TErrorAttribute::TValue>>;
-template <class T>
-struct TMergeableDictionaryImpl
-{
- // TL;DR: MakeIterableView returns something like std::span<std::pair<TKey, TValue>>.
- using TView = std::invoke_result_t<decltype(&TMergeDictionariesTraits<T>::MakeIterableView), const T&>;
- using TIterator = std::ranges::iterator_t<TView>;
- using TValue = typename std::iterator_traits<TIterator>::value_type;
+} // namespace NMergeableRangeImpl
- static constexpr bool ValidSize = requires {
- { std::tuple_size<TValue>::value } -> std::same_as<const size_t&>;
- } && (std::tuple_size<TValue>::value == 2);
+////////////////////////////////////////////////////////////////////////////////
- static constexpr bool CorrectTupleElements = requires {
- typename std::tuple_element<0, TValue>::type;
- std::same_as<typename std::tuple_element<0, TValue>::type, TErrorAttribute::TKey>;
+// Can be customized to make your dictionary satisfy CMergeableDictionary.
+inline constexpr NMergeableRangeImpl::TFn AsMergeableRange = {};
- typename std::tuple_element<1, TValue>::type;
- std::same_as<typename std::tuple_element<1, TValue>::type, TErrorAttribute::TValue>;
- };
-};
+////////////////////////////////////////////////////////////////////////////////
-} // namespace NDetail
+template <class T>
+concept CMergeableDictionary = CTagInvocableS<
+ TTagInvokeTag<AsMergeableRange>,
+ NMergeableRangeImpl::TMergeableRange(const T&)>;
////////////////////////////////////////////////////////////////////////////////
-template <class T>
-concept CMergeableDictionary =
- requires (const T& dict) {
- TMergeDictionariesTraits<T>::MakeIterableView(dict);
- } &&
- NDetail::TMergeableDictionaryImpl<T>::ValidSize &&
- NDetail::TMergeableDictionaryImpl<T>::CorrectTupleElements;
+using TAnyMergeableDictionaryRef = TAnyRef<
+ TOverload<AsMergeableRange, NMergeableRangeImpl::TMergeableRange(const TErasedThis&)>>;
////////////////////////////////////////////////////////////////////////////////
diff --git a/library/cpp/yt/string/string.cpp b/library/cpp/yt/string/string.cpp
index 3cf5651e230..b9c32361343 100644
--- a/library/cpp/yt/string/string.cpp
+++ b/library/cpp/yt/string/string.cpp
@@ -362,7 +362,8 @@ TStringBuf FormatBool(bool value)
void TruncateStringInplace(TString* string, int lengthLimit, TStringBuf truncatedSuffix)
{
if (std::ssize(*string) > lengthLimit) {
- *string = Format("%v%v", string->substr(0, lengthLimit), truncatedSuffix);
+ string->resize(lengthLimit);
+ string->append(truncatedSuffix);
}
}