diff --git a/rust/cargo/Cargo.toml b/rust/cargo/Cargo.toml index 7a389e764eea..2ed875695d37 100644 --- a/rust/cargo/Cargo.toml +++ b/rust/cargo/Cargo.toml @@ -11,6 +11,7 @@ version = "4.27.3-beta.0" edition = "2021" links = "libupb" license = "BSD-3-Clause" +rust-version = "1.74" [lib] path = "src/shared.rs" diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index c3ae2039b3d6..571e2f535bd9 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -225,9 +225,6 @@ class WireFormatLite; class WeakFieldMap; class RustMapHelper; -template -class GenericTypeHandler; // defined in repeated_field.h - // We compute sizes as size_t but cache them as int. This function converts a // computed size to a cached size. Since we don't proceed with serialization // if the total size was > INT_MAX, it is not important what this function @@ -963,8 +960,6 @@ class PROTOBUF_EXPORT MessageLite { template friend class Arena::InternalHelper; - template - friend class internal::GenericTypeHandler; friend auto internal::GetClassData(const MessageLite& msg); diff --git a/src/google/protobuf/repeated_field.h b/src/google/protobuf/repeated_field.h index 4c4ae30808ea..f885f49d594b 100644 --- a/src/google/protobuf/repeated_field.h +++ b/src/google/protobuf/repeated_field.h @@ -28,7 +28,6 @@ #include #include #include -#include #include #include @@ -36,7 +35,6 @@ #include "absl/base/dynamic_annotations.h" #include "absl/base/optimization.h" #include "absl/log/absl_check.h" -#include "absl/log/absl_log.h" #include "absl/meta/type_traits.h" #include "absl/strings/cord.h" #include "google/protobuf/arena.h" @@ -46,7 +44,6 @@ #include "google/protobuf/port.h" #include "google/protobuf/repeated_ptr_field.h" - // Must be included last. #include "google/protobuf/port_def.inc" @@ -79,7 +76,11 @@ constexpr int RepeatedFieldLowerClampLimit() { // overflows when multiplied by 2 (which is undefined behavior). Sizes above // this will clamp to the maximum int value instead of following exponential // growth when growing a repeated field. +#if defined(__cpp_inline_variables) +inline constexpr int kRepeatedFieldUpperClampLimit = +#else constexpr int kRepeatedFieldUpperClampLimit = +#endif (std::numeric_limits::max() / 2) + 1; template diff --git a/src/google/protobuf/repeated_field_unittest.cc b/src/google/protobuf/repeated_field_unittest.cc index 551b135811f7..e7b4e4b9318e 100644 --- a/src/google/protobuf/repeated_field_unittest.cc +++ b/src/google/protobuf/repeated_field_unittest.cc @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -34,7 +33,6 @@ #include #include "absl/log/absl_check.h" #include "absl/numeric/bits.h" -#include "absl/random/random.h" #include "absl/strings/cord.h" #include "absl/strings/str_cat.h" #include "absl/types/span.h" diff --git a/src/google/protobuf/repeated_ptr_field.cc b/src/google/protobuf/repeated_ptr_field.cc index 891439c14d25..f26e32fd55f3 100644 --- a/src/google/protobuf/repeated_ptr_field.cc +++ b/src/google/protobuf/repeated_ptr_field.cc @@ -16,9 +16,9 @@ #include #include #include +#include #include -#include "absl/base/prefetch.h" #include "absl/log/absl_check.h" #include "google/protobuf/arena.h" #include "google/protobuf/message_lite.h" diff --git a/src/google/protobuf/repeated_ptr_field.h b/src/google/protobuf/repeated_ptr_field.h index 12bfdd77a4c2..2286a9f24752 100644 --- a/src/google/protobuf/repeated_ptr_field.h +++ b/src/google/protobuf/repeated_ptr_field.h @@ -26,8 +26,8 @@ #include #include #include +#include #include -#include #include #include @@ -40,7 +40,6 @@ #include "google/protobuf/message_lite.h" #include "google/protobuf/port.h" - // Must be included last. #include "google/protobuf/port_def.inc" @@ -92,9 +91,13 @@ struct IsMovable // Do not use this struct - it exists for internal use only. template struct ArenaOffsetHelper { - constexpr static size_t value = offsetof(T, arena_); + static constexpr size_t value = offsetof(T, arena_); }; +// Defined further below. +template +class GenericTypeHandler; + // This is the common base class for RepeatedPtrFields. It deals only in void* // pointers. Users should not use this interface directly. // @@ -102,11 +105,16 @@ struct ArenaOffsetHelper { // but may have a template argument called TypeHandler. Its signature is: // class TypeHandler { // public: -// typedef MyType Type; -// static Type* New(); -// static Type* NewFromPrototype(const Type* prototype, -// Arena* arena); -// static void Delete(Type*); +// using Type = MyType; +// using Movable = ...; +// +// static Type*(*)(Arena*) GetNewFunc(); +// static void GetArena(Type* value); +// +// static Type* New(Arena* arena); +// static Type* New(Arena* arena, Type&& value); +// static Type* NewFromPrototype(const Type* prototype, Arena* arena); +// static void Delete(Type*, Arena* arena); // static void Clear(Type*); // static void Merge(const Type& from, Type* to); // @@ -114,20 +122,20 @@ struct ArenaOffsetHelper { // static int SpaceUsedLong(const Type&); // }; class PROTOBUF_EXPORT RepeatedPtrFieldBase { - template - using Value = typename Handler::Type; + template + using Value = typename TypeHandler::Type; static constexpr int kSSOCapacity = 1; using ElementFactory = void* (*)(Arena*); protected: - // We use the same Handler for all Message types to deduplicate generated + // We use the same TypeHandler for all Message types to deduplicate generated // code. - template + template using CommonHandler = typename std::conditional< - std::is_base_of>::value, - internal::GenericTypeHandler, Handler>::type; + std::is_base_of>::value, + GenericTypeHandler, TypeHandler>::type; constexpr RepeatedPtrFieldBase() : tagged_rep_or_elem_(nullptr), @@ -145,7 +153,7 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { ~RepeatedPtrFieldBase() { #ifndef NDEBUG - // Try to trigger segfault / asan failure in non-opt builds. If arena_ + // Try to trigger segfault / asan failure in non-opt builds if arena_ // lifetime has ended before the destructor. if (arena_) (void)arena_->SpaceAllocated(); #endif @@ -182,12 +190,12 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { return cast(element_at(index)); } - template - Value* Add() { - if (std::is_same, std::string>{}) { - return cast(AddString()); + template + Value* Add() { + if (std::is_same, std::string>{}) { + return cast(AddString()); } - return cast(AddMessageLite(Handler::GetNewFunc())); + return cast(AddMessageLite(TypeHandler::GetNewFunc())); } template < @@ -318,9 +326,9 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { template void CopyFrom(const RepeatedPtrFieldBase& other) { if (&other == this) return; - RepeatedPtrFieldBase::Clear(); + Clear(); if (other.empty()) return; - RepeatedPtrFieldBase::MergeFrom(other); + MergeFrom(other); } void CloseGap(int start, int num); @@ -767,8 +775,8 @@ void RepeatedPtrFieldBase::MergeFrom( const RepeatedPtrFieldBase& from); -template -void* RepeatedPtrFieldBase::AddInternal(F factory) { +template +void* RepeatedPtrFieldBase::AddInternal(Factory factory) { Arena* const arena = GetArena(); if (tagged_rep_or_elem_ == nullptr) { ExchangeCurrentSize(1); @@ -808,27 +816,30 @@ PROTOBUF_EXPORT void InternalOutOfLineDeleteMessageLite(MessageLite* message); template class GenericTypeHandler { public: - typedef GenericType Type; - using Movable = IsMovable; + using Type = GenericType; + using Movable = IsMovable; static constexpr auto GetNewFunc() { return Arena::DefaultConstruct; } + static inline Arena* GetArena(Type* value) { + return Arena::InternalGetArena(value); + } - static inline GenericType* New(Arena* arena) { - return static_cast(Arena::DefaultConstruct(arena)); + static inline Type* New(Arena* arena) { + return static_cast(Arena::DefaultConstruct(arena)); } - static inline GenericType* New(Arena* arena, GenericType&& value) { - return Arena::Create(arena, std::move(value)); + static inline Type* New(Arena* arena, Type&& value) { + return Arena::Create(arena, std::move(value)); } - static inline GenericType* NewFromPrototype(const GenericType* /*prototype*/, - Arena* arena = nullptr) { + static inline Type* NewFromPrototype(const Type* /*prototype*/, + Arena* arena = nullptr) { return New(arena); } - static inline void Delete(GenericType* value, Arena* arena) { + static inline void Delete(Type* value, Arena* arena) { if (arena != nullptr) return; #ifdef __cpp_if_constexpr - if constexpr (std::is_base_of::value) { + if constexpr (std::is_base_of::value) { // Using virtual destructor to reduce generated code size that would have - // happened otherwise due to inlined `~GenericType`. + // happened otherwise due to inlined `~Type()`. InternalOutOfLineDeleteMessageLite(value); } else { delete value; @@ -837,13 +848,9 @@ class GenericTypeHandler { delete value; #endif } - static inline Arena* GetArena(GenericType* value) { - return Arena::InternalGetArena(value); - } - - static inline void Clear(GenericType* value) { value->Clear(); } - static void Merge(const GenericType& from, GenericType* to); - static inline size_t SpaceUsedLong(const GenericType& value) { + static inline void Clear(Type* value) { value->Clear(); } + static void Merge(const Type& from, Type* to); + static inline size_t SpaceUsedLong(const Type& value) { return value.SpaceUsedLong(); } }; @@ -887,32 +894,29 @@ PROTOBUF_EXPORT void* NewStringElement(Arena* arena); template <> class GenericTypeHandler { public: - typedef std::string Type; + using Type = std::string; using Movable = IsMovable; static constexpr auto GetNewFunc() { return NewStringElement; } + static inline Arena* GetArena(Type*) { return nullptr; } - static PROTOBUF_NOINLINE std::string* New(Arena* arena) { - return Arena::Create(arena); + static PROTOBUF_NOINLINE Type* New(Arena* arena) { + return Arena::Create(arena); } - static PROTOBUF_NOINLINE std::string* New(Arena* arena, std::string&& value) { - return Arena::Create(arena, std::move(value)); + static PROTOBUF_NOINLINE Type* New(Arena* arena, Type&& value) { + return Arena::Create(arena, std::move(value)); } - static inline std::string* NewFromPrototype(const std::string*, - Arena* arena) { + static inline Type* NewFromPrototype(const Type*, Arena* arena) { return New(arena); } - static inline Arena* GetArena(std::string*) { return nullptr; } - static inline void Delete(std::string* value, Arena* arena) { + static inline void Delete(Type* value, Arena* arena) { if (arena == nullptr) { delete value; } } - static inline void Clear(std::string* value) { value->clear(); } - static inline void Merge(const std::string& from, std::string* to) { - *to = from; - } - static size_t SpaceUsedLong(const std::string& value) { + static inline void Clear(Type* value) { value->clear(); } + static inline void Merge(const Type& from, Type* to) { *to = from; } + static size_t SpaceUsedLong(const Type& value) { return sizeof(value) + StringSpaceUsedExcludingSelfLong(value); } };