Protobuf Rust: simplify the MakeCleanup() helper function

By changing the return type to `auto`, we can handle `std::string` and other
types in a single definition without needing a separate overload.

PiperOrigin-RevId: 653272253
pull/17508/head
Adam Cozzette 7 months ago committed by Copybara-Service
parent 08ee87fcc6
commit efb57278a9
  1. 13
      rust/cpp_kernel/map.h

@ -2,6 +2,7 @@
#define GOOGLE_PROTOBUF_RUST_CPP_KERNEL_MAP_H__
#include <memory>
#include <type_traits>
namespace google {
namespace protobuf {
@ -13,12 +14,12 @@ namespace rust {
// for std::string pointers it returns a std::unique_ptr to take ownership of
// the raw pointer.
template <typename T>
int MakeCleanup(T value) {
return 0;
}
inline std::unique_ptr<std::string> MakeCleanup(std::string* value) {
return std::unique_ptr<std::string>(value);
auto MakeCleanup(T value) {
if constexpr (std::is_same<T, std::string*>::value) {
return std::unique_ptr<std::string>(value);
} else {
return 0;
}
}
} // namespace rust

Loading…
Cancel
Save