Revert "Make android::base:Result more code-size-efficient."
This reverts commit 3142bd70939fd266c5542e1c2da7a1301f04e955.
Reason for revert: likely cause of b/336184491
Change-Id: I469f2a509bb391ea09670d2f945633750a58eb3c
diff --git a/Android.bp b/Android.bp
index 682f7a5..5176046 100644
--- a/Android.bp
+++ b/Android.bp
@@ -98,7 +98,6 @@
"posix_strerror_r.cpp",
"process.cpp",
"properties.cpp",
- "result.cpp",
"stringprintf.cpp",
"strings.cpp",
"threads.cpp",
diff --git a/include/android-base/errors.h b/include/android-base/errors.h
index ab584d4..99029d1 100644
--- a/include/android-base/errors.h
+++ b/include/android-base/errors.h
@@ -139,10 +139,8 @@
// template <typename U>
// operator Result<U, E>()&& { return val_.error(); }
- // And there needs to be a method that returns the string representation of the fail value.
- // static const std::string& ErrorMessage(const T& v);
- // or
- // static std::string ErrorMessage(const T& v);
+ // Returns the string representation of the fail value.
+ static std::string ErrorMessage(const T& v);
};
} // namespace base
diff --git a/include/android-base/result.h b/include/android-base/result.h
index 00abab4..8ac7d9e 100644
--- a/include/android-base/result.h
+++ b/include/android-base/result.h
@@ -93,7 +93,6 @@
#include <assert.h>
#include <errno.h>
-#include <string.h>
#include <sstream>
#include <string>
@@ -114,7 +113,7 @@
Errno(int e) : val_(e) {}
int value() const { return val_; }
operator int() const { return value(); }
- const char* print() const { return strerror(value()); }
+ std::string print() const { return strerror(value()); }
int val_;
@@ -132,21 +131,10 @@
ResultError(T&& message, P&& code)
: message_(std::forward<T>(message)), code_(E(std::forward<P>(code))) {}
- ResultError(const ResultError& other) = default;
- ResultError(ResultError&& other) = default;
- ResultError& operator=(const ResultError& other) = default;
- ResultError& operator=(ResultError&& other) = default;
-
template <typename T>
// NOLINTNEXTLINE(google-explicit-constructor)
- operator android::base::expected<T, ResultError<E>>() && {
- return android::base::unexpected(std::move(*this));
- }
-
- template <typename T>
- // NOLINTNEXTLINE(google-explicit-constructor)
- operator android::base::expected<T, ResultError<E>>() const& {
- return android::base::unexpected(*this);
+ operator android::base::expected<T, ResultError<E>>() const {
+ return android::base::unexpected(ResultError<E>(message_, code_));
}
const std::string& message() const { return message_; }
@@ -297,19 +285,15 @@
return ErrorCode(code, args...);
}
-__attribute__((noinline)) ResultError<Errno> MakeResultErrorWithCode(std::string&& message,
- Errno code);
-
template <typename... Args>
-inline ResultError<Errno> ErrorfImpl(fmt::format_string<Args...> fmt, const Args&... args) {
- return ResultError(fmt::vformat(fmt.get(), fmt::make_format_args(args...)),
- ErrorCode(Errno{}, args...));
+inline Error<Errno> ErrorfImpl(fmt::format_string<Args...> fmt, const Args&... args) {
+ return Error(false, ErrorCode(Errno{}, args...),
+ fmt::vformat(fmt.get(), fmt::make_format_args(args...)));
}
template <typename... Args>
-inline ResultError<Errno> ErrnoErrorfImpl(fmt::format_string<Args...> fmt, const Args&... args) {
- return MakeResultErrorWithCode(fmt::vformat(fmt.get(), fmt::make_format_args(args...)),
- Errno{errno});
+inline Error<Errno> ErrnoErrorfImpl(fmt::format_string<Args...> fmt, const Args&... args) {
+ return Error<Errno>(true, Errno{errno}, fmt::vformat(fmt.get(), fmt::make_format_args(args...)));
}
#define Errorf(fmt, ...) android::base::ErrorfImpl(FMT_STRING(fmt), ##__VA_ARGS__)
@@ -339,10 +323,14 @@
struct ConversionBase {
ErrorType<T> error_;
// T is a expected<U, ErrorType<T>>.
- operator T() const& { return unexpected(error_); }
- operator T() && { return unexpected(std::move(error_)); }
+ operator const T() const && {
+ return unexpected(std::move(error_));
+ }
- operator Code<T>() const { return error_.code(); }
+ operator const Code<T>() const && {
+ return error_.code();
+ }
+
};
// User defined conversions can be followed by numeric conversions
@@ -357,9 +345,9 @@
> : public ConversionBase<T>
{
#pragma push_macro("SPECIALIZED_CONVERSION")
-#define SPECIALIZED_CONVERSION(type) \
- operator expected<type, ErrorType<T>>() const& { return unexpected(this->error_); } \
- operator expected<type, ErrorType<T>>()&& { return unexpected(std::move(this->error_)); }
+#define SPECIALIZED_CONVERSION(type)\
+ operator const expected<type, ErrorType<T>>() const &&\
+ { return unexpected(std::move(this->error_));}
SPECIALIZED_CONVERSION(int)
SPECIALIZED_CONVERSION(short int)
@@ -391,9 +379,6 @@
// Define a concept which **any** type matches to
concept Universal = std::is_same_v<U, U>;
#endif
-
-// A type that is never used.
-struct Never {};
} // namespace impl
template <typename T, typename E, bool include_message>
@@ -421,22 +406,16 @@
}
// Consumes V when it's a fail value
- static OkOrFail<V> Fail(V&& v) {
+ static const OkOrFail<V> Fail(V&& v) {
assert(!IsOk(v));
return OkOrFail<V>{std::move(v.error())};
}
- // We specialize as much as possible to avoid ambiguous conversion with templated expected ctor.
- // We don't need this specialization if `C` is numeric because that case is already covered by
- // `NumericConversions`.
- operator Result<std::conditional_t<impl::IsNumeric<C>, impl::Never, C>, E, include_message>()
- const& {
- return unexpected(this->error_);
- }
- operator Result<std::conditional_t<impl::IsNumeric<C>, impl::Never, C>, E, include_message>() && {
+ // We specialize as much as possible to avoid ambiguous conversion with
+ // templated expected ctor
+ operator const Result<C, E, include_message>() const && {
return unexpected(std::move(this->error_));
}
-
#ifdef __cpp_concepts
// The idea here is to match this template method to any type (not simply trivial types).
// The reason for including a constraint is to take advantage of the fact that a constrained
@@ -444,25 +423,14 @@
// specialization rules (thus avoiding ambiguity). So we use a universally matching constraint to
// mark this function as less preferable (but still accepting of all types).
template <impl::Universal U>
- operator Result<U, E, include_message>() const& {
- return unexpected(this->error_);
- }
- template <impl::Universal U>
- operator Result<U, E, include_message>() && {
- return unexpected(std::move(this->error_));
- }
#else
template <typename U>
- operator Result<U, E, include_message>() const& {
- return unexpected(this->error_);
- }
- template <typename U>
- operator Result<U, E, include_message>() && {
+#endif
+ operator const Result<U, E, include_message>() const&& {
return unexpected(std::move(this->error_));
}
-#endif
- static const std::string& ErrorMessage(const V& val) { return val.error().message(); }
+ static std::string ErrorMessage(const V& val) { return val.error().message(); }
};
// Macros for testing the results of functions that return android::base::Result.
diff --git a/result.cpp b/result.cpp
deleted file mode 100644
index 84535d6..0000000
--- a/result.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (C) 2024 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "android-base/result.h"
-
-namespace android {
-namespace base {
-
-ResultError<Errno> MakeResultErrorWithCode(std::string&& message, Errno code) {
- return ResultError(std::move(message) + ": " + code.print(), code);
-}
-
-} // namespace base
-} // namespace android