@@ -21,7 +21,9 @@ namespace rfl {
2121class Error {
2222 public:
2323 Error (const std::string& _what) : what_(_what) {}
24+
2425 Error (const Error& e) = default ;
26+
2527 Error& operator =(const Error&) = default ;
2628
2729 // / Returns the error message, equivalent to .what() in std::exception.
@@ -50,13 +52,21 @@ using Result = std::expected<T, rfl::Error>;
5052template <class E >
5153struct Unexpected {
5254 Unexpected (E&& _err) : err_{std::forward<E>(_err)} {}
55+
5356 Unexpected (const E& _err) : err_{_err} {}
57+
5458 Unexpected (Unexpected&&) = default ;
59+
5560 Unexpected (const Unexpected&) = default ;
61+
5662 Unexpected& operator =(Unexpected&&) = default ;
63+
5764 Unexpected& operator =(const Unexpected&) = default ;
65+
5866 const E& error () const & { return err_; }
67+
5968 E&& error() && { return std::move (err_); }
69+
6070 E& error () & { return err_; }
6171
6272 private:
@@ -88,10 +98,6 @@ class Result {
8898 new (&get_err ()) Error (std::move (_err.error ()));
8999 }
90100
91- // Result(Error&& _err) noexcept : success_(false) {
92- // new (&get_err()) Error(std::move(_err));
93- // }
94-
95101 Result (Result<T>&& _other) noexcept : success_(_other.success_) {
96102 move_from_other (_other);
97103 }
@@ -119,7 +125,7 @@ class Result {
119125
120126 // / Monadic operation - F must be a function of type T -> Result<U>.
121127 template <class F >
122- auto and_then (F& & _f) && {
128+ auto and_then (const F & _f) && {
123129 // / Result_U is expected to be of type Result<U>.
124130 using Result_U = typename std::invoke_result<F, T>::type;
125131 if (success_) {
@@ -257,6 +263,16 @@ class Result {
257263 }
258264 }
259265
266+ // / Returns the value if the result does not contain an error, throws an
267+ // / exceptions if not. Similar to .unwrap() in Rust.
268+ T& value () & {
269+ if (success_) {
270+ return get_t ();
271+ } else {
272+ throw std::runtime_error (get_err ().what ());
273+ }
274+ }
275+
260276 // / Returns the value if the result does not contain an error, throws an
261277 // / exceptions if not. Similar to .unwrap() in Rust.
262278 const T& value () const & {
@@ -308,14 +324,19 @@ class Result {
308324
309325 bool has_value () const noexcept { return success_; }
310326
311- const Error& error () const & {
327+ Error& error () && {
328+ if (success_) throw std::runtime_error (" Expected does not contain value" );
329+ return std::move (*this ).get_err ();
330+ }
331+
332+ Error& error () & {
312333 if (success_) throw std::runtime_error (" Expected does not contain value" );
313334 return get_err ();
314335 }
315336
316- Error& error () & & {
337+ const Error& error () const & {
317338 if (success_) throw std::runtime_error (" Expected does not contain value" );
318- return std::move (* this ). get_err ();
339+ return get_err ();
319340 }
320341
321342 T* operator ->() noexcept { return &get_t (); }
0 commit comments