nit: nullptr is dead code, otherwise the above operator== would be UB. So if you retouch:
os << "NoCopy(" << *o.m_n << ")";
Also: clang-format for me:
diff --git a/src/test/result_tests.cpp b/src/test/result_tests.cpp
index 847f68121d..6d688f7b74 100644
--- a/src/test/result_tests.cpp
+++ b/src/test/result_tests.cpp
@@ -18,8 +18,7 @@ inline std::ostream& operator<<(std::ostream& os, const bilingual_str& s)
BOOST_AUTO_TEST_SUITE(result_tests)
-struct NoCopy
-{
+struct NoCopy {
NoCopy(int n) : m_n{std::make_unique<int>(n)} {}
std::unique_ptr<int> m_n;
};
@@ -31,7 +30,10 @@ bool operator==(const NoCopy& a, const NoCopy& b)
std::ostream& operator<<(std::ostream& os, const NoCopy& o)
{
- if (o.m_n) os << "NoCopy(" << *o.m_n << ")"; else os << "NoCopy(nullptr)";
+ if (o.m_n)
+ os << "NoCopy(" << *o.m_n << ")";
+ else
+ os << "NoCopy(nullptr)";
return os;
}
@@ -53,14 +55,14 @@ util::Result<NoCopy> NoCopyFn(int i, bool success)
return {util::Error{Untranslated(strprintf("nocopy %i error.", i))}};
}
-template<typename T>
+template <typename T>
void ExpectResult(const util::Result<T>& result, bool success, const bilingual_str& str)
{
BOOST_CHECK_EQUAL(bool(result), success);
BOOST_CHECK_EQUAL(util::ErrorString(result), str);
}
-template<typename T, typename... Args>
+template <typename T, typename... Args>
void ExpectSuccess(const util::Result<T>& result, const bilingual_str& str, Args&&... args)
{
ExpectResult(result, true, str);
@@ -69,7 +71,7 @@ void ExpectSuccess(const util::Result<T>& result, const bilingual_str& str, Args
BOOST_CHECK_EQUAL(&result.value(), &*result);
}
-template<typename T, typename... Args>
+template <typename T, typename... Args>
void ExpectFail(const util::Result<T>& result, const bilingual_str& str)
{
ExpectResult(result, false, str);
diff --git a/src/util/result.h b/src/util/result.h
index 8f100b7933..a87627a589 100644
--- a/src/util/result.h
+++ b/src/util/result.h
@@ -12,7 +12,9 @@
namespace util {
-struct Error { bilingual_str message; };
+struct Error {
+ bilingual_str message;
+};
//! The util::Result class provides a standard way for functions to return
//! either error messages or result values.
@@ -29,8 +31,9 @@ struct Error { bilingual_str message; };
//! `std::optional<T>` can be updated to return `util::Result<T>` and return
//! error strings usually just replacing `return std::nullopt;` with `return
//! util::Error{error_string};`.
-template<class T>
-class Result {
+template <class T>
+class Result
+{
private:
std::variant<bilingual_str, T> m_variant;
@@ -44,17 +47,31 @@ public:
//! std::optional methods, so functions returning optional<T> can change to
//! return Result<T> with minimal changes to existing code, and vice versa.
bool has_value() const { return m_variant.index() == 1; }
- const T& value() const LIFETIMEBOUND { assert(*this); return std::get<1>(m_variant); }
- T& value() LIFETIMEBOUND { assert(*this); return std::get<1>(m_variant); }
+ const T& value() const LIFETIMEBOUND
+ {
+ assert(*this);
+ return std::get<1>(m_variant);
+ }
+ T& value() LIFETIMEBOUND
+ {
+ assert(*this);
+ return std::get<1>(m_variant);
+ }
explicit operator bool() const { return has_value(); }
const T* operator->() const LIFETIMEBOUND { return &value(); }
const T& operator*() const LIFETIMEBOUND { return value(); }
T* operator->() LIFETIMEBOUND { return &value(); }
T& operator*() LIFETIMEBOUND { return value(); }
template <class U>
- T value_or(U&& default_value) const& { return has_value() ? value() : std::forward<U>(default_value); }
+ T value_or(U&& default_value) const&
+ {
+ return has_value() ? value() : std::forward<U>(default_value);
+ }
template <class U>
- T value_or(U&& default_value) && { return has_value() ? std::move(value()) : std::forward<U>(default_value); }
+ T value_or(U&& default_value) &&
+ {
+ return has_value() ? std::move(value()) : std::forward<U>(default_value);
+ }
};
template <typename T>