Move result.h from init to libbase

The Result, Error, ErrnoError are quite generic. Moving them from init
to libbase so that they can be used from other places.

Bug: 132145659
Test: libbase_test
Change-Id: Id774a587f74380fadd7a0fc88c0aa892c3d9a489
diff --git a/init/result.h b/init/result.h
index baa680d..984b257 100644
--- a/init/result.h
+++ b/init/result.h
@@ -29,8 +29,8 @@
 // string value.
 //
 // Success is a typedef that aids in creating Result<T> that do not contain a return value.
-// Result<Success> is the correct return type for a function that either returns successfully or
-// returns an error value.  Returning Success() from a function that returns Result<Success> is the
+// Result<Nothing> is the correct return type for a function that either returns successfully or
+// returns an error value.  Returning Nothing() from a function that returns Result<Nothing> is the
 // correct way to indicate that a function without a return type has completed successfully.
 //
 // A successful Result<T> is constructed implicitly from any type that can be implicitly converted
@@ -70,101 +70,10 @@
 
 #pragma once
 
-#include <errno.h>
+#include <android-base/result.h>
 
-#include <sstream>
-#include <string>
-
-#include <android-base/expected.h>
-
-namespace android {
-namespace init {
-
-struct ResultError {
-    template <typename T>
-    ResultError(T&& error_string, int error_errno)
-        : as_string(std::forward<T>(error_string)), as_errno(error_errno) {}
-
-    template <typename T>
-    operator android::base::expected<T, ResultError>() {
-        return android::base::unexpected(ResultError(as_string, as_errno));
-    }
-
-    std::string as_string;
-    int as_errno;
-};
-
-inline std::ostream& operator<<(std::ostream& os, const ResultError& t) {
-    os << t.as_string;
-    return os;
-}
-
-inline std::ostream& operator<<(std::ostream& os, ResultError&& t) {
-    os << std::move(t.as_string);
-    return os;
-}
-
-class Error {
-  public:
-    Error() : errno_(0), append_errno_(false) {}
-    Error(int errno_to_append) : errno_(errno_to_append), append_errno_(true) {}
-
-    template <typename T>
-    operator android::base::expected<T, ResultError>() {
-        return android::base::unexpected(ResultError(str(), errno_));
-    }
-
-    template <typename T>
-    Error&& operator<<(T&& t) {
-        ss_ << std::forward<T>(t);
-        return std::move(*this);
-    }
-
-    Error&& operator<<(const ResultError& result_error) {
-        ss_ << result_error.as_string;
-        errno_ = result_error.as_errno;
-        return std::move(*this);
-    }
-
-    Error&& operator<<(ResultError&& result_error) {
-        ss_ << std::move(result_error.as_string);
-        errno_ = result_error.as_errno;
-        return std::move(*this);
-    }
-
-    const std::string str() const {
-        std::string str = ss_.str();
-        if (append_errno_) {
-            if (str.empty()) {
-                return strerror(errno_);
-            }
-            return str + ": " + strerror(errno_);
-        }
-        return str;
-    }
-
-    int get_errno() const { return errno_; }
-
-    Error(const Error&) = delete;
-    Error(Error&&) = delete;
-    Error& operator=(const Error&) = delete;
-    Error& operator=(Error&&) = delete;
-
-  private:
-    std::stringstream ss_;
-    int errno_;
-    bool append_errno_;
-};
-
-inline Error ErrnoError() {
-    return Error(errno);
-}
-
-template <typename T>
-using Result = android::base::expected<T, ResultError>;
-
-using Success = std::monostate;
-
-}  // namespace init
-}  // namespace android
-
+using android::base::ErrnoError;
+using android::base::Error;
+using android::base::Result;
+using android::base::ResultError;
+using android::base::Success;