aboutsummaryrefslogtreecommitdiff
path: root/third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/result.h
blob: 86bbb9041fa4fc89e70f44c98a2ca91ab00937cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIB_FIT_RESULT_H_
#define LIB_FIT_RESULT_H_

#include <lib/fit/internal/compiler.h>
#include <lib/fit/internal/result.h>

#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>

// General purpose fit::result type for Zircon kernel, system, and above.
//
// fit::result is an efficient C++ implementation of the result pattern found in many languages and
// vocabulary libraries. This implementation supports returning either an error value or zero/one
// non-error values from a function or method.
//
// To make a fit::result:
//
//   fit::success(success_value)  // Success for fit::result<E, V>.
//   fit::success()               // Success for fit::result<E> (no success value).
//   fit::ok(success_value)       // Success for fit::result<E, V>.
//   fit::ok()                    // Success for fit::result<E> (no success value).
//
//   fit::error(error_value)      // Failure.
//   fit::as_error(error_value)   // Failure.
//   fit::failed()                // Failure for fit::result<>.
//
// General functions that can always be called:
//
//   bool is_ok()
//   bool is_error()
//   T value_or(default_value)    // Returns value on success, or default on failure.
//   result<E[, V]> map_error(fn) // Transforms the error value of the result using fn.
//
// Available only when is_ok() (will assert otherwise).
//
//   T& value()                  // Accesses the value.
//   T&& value()                 // Moves the value.
//   T& operator*()              // Accesses the value.
//   T&& operator*()             // Moves the value.
//   T* operator->()             // Accesses the value.
//   success<T> take_value()     // Generates a fit::success() which can be implicitly converted to
//                               // another fit::result with the same "success" type.
//
// Available only when is_error() (will assert otherwise):
//
//   E& error_value()            // Error value.
//   E&& error_value()           // Error value.
//   error<E> take_error()       // Generates a fit::error() which can be implicitly converted to a
//                               // fit::result with a different "success" vluae type (or
//                               // fit::result<E>).

#include "pw_assert/assert.h"

namespace fit {

// Convenience type to indicate failure without elaboration.
//
// Example:
//
//   fit::result<fit::failed> Contains(const char* string, const char* find) {
//     if (string == nullptr || find == nullptr ||
//         strstr(string, find) == nullptr) {
//       return fit::failed();
//     }
//     return fit::ok();
//   }
//
struct failed {};

// Type representing an error value of type E to return as a result. Returning an error through
// fit::result always requires using fit::error to disambiguate errors from values.
//
// fit::result<E, Ts...> is implicitly constructible from any fit::error<F>, where E is
// constructible from F. This simplifies returning errors when the E has converting constructors.
//
// Example usage:
//
//   fit::result<std::string, size_t> StringLength(const char* string) {
//     if (string == nullptr) {
//       return fit::error("Argument to StringLength is nullptr!");
//     }
//     return fit::success(strlen(string));
//   }
//
template <typename E>
class error {
 public:
  using error_type = E;

  // Constructs an error with the given arguments.
  template <typename... Args,
            ::fit::internal::requires_conditions<std::is_constructible<E, Args...>> = true>
  explicit constexpr error(Args&&... args) : value_(std::forward<Args>(args)...) {}

  ~error() = default;

  // Error has the same copyability and moveability as the underlying type E.
  constexpr error(const error&) = default;
  constexpr error& operator=(const error&) = default;
  constexpr error(error&&) = default;
  constexpr error& operator=(error&&) = default;

 private:
  template <typename F, typename... Ts>
  friend class result;

  E value_;
};

#if __cplusplus >= 201703L

// Deduction guide to simplify single argument error expressions in C++17.
template <typename T>
error(T) -> error<T>;

#endif

// Returns fit::error<E> for the given value, where E is deduced from the argument type. This
// utility is a C++14 compatible alternative to the C++17 deduction guide above.
//
// Example:
//
//   fit::result<std::string, std::string> MakeString(const char* string) {
//     if (string == nullptr) {
//       return fit::as_error("String is nullptr!");
//     } else if (strlen(string) == 0) {
//       return fit::as_error("String is empty!");
//     }
//     return fit::ok(string);
//   }
//
template <typename E>
constexpr error<std::decay_t<E>> as_error(E&& error_value) {
  return error<std::decay_t<E>>(std::forward<E>(error_value));
}

// Type representing success with zero or one value.
//
// Base type.
template <typename... Ts>
class success;

// Type representing a success value of type T to return as a result. Returning a value through
// fit::result always requires using fit::success to disambiguate errors from values.
//
// fit::result<E, T> is implicitly constructible from any fit::success<U>, where T is
// constructible from U. This simplifies returning values when T has converting constructors.
template <typename T>
class success<T> {
 public:
  using value_type = T;

  // Constructs a success value with the given arguments.
  template <typename... Args,
            ::fit::internal::requires_conditions<std::is_constructible<T, Args...>> = true>
  explicit constexpr success(Args&&... args) : value_(std::forward<Args>(args)...) {}

  ~success() = default;

  // Error has the same copyability and moveability as the underlying type E.
  constexpr success(const success&) = default;
  constexpr success& operator=(const success&) = default;
  constexpr success(success&&) = default;
  constexpr success& operator=(success&&) = default;

 private:
  template <typename E, typename... Ts>
  friend class result;

  T value_;
};

// Specialization of success for empty values.
template <>
class success<> {
 public:
  constexpr success() = default;
  ~success() = default;

  constexpr success(const success&) = default;
  constexpr success& operator=(const success&) = default;
  constexpr success(success&&) = default;
  constexpr success& operator=(success&&) = default;
};

#if __cplusplus >= 201703L

// Deduction guides to simplify zero and single argument success expressions in C++17.
success() -> success<>;

template <typename T>
success(T) -> success<T>;

#endif

// Returns fit::success<T> for the given value, where T is deduced from the argument type. This
// utility is a C++14 compatible alternative to the C++17 deduction guide above.
//
// Example:
//
//   fit::result<std::string, std::string> MakeString(const char* string) {
//     if (string == nullptr) {
//       return fit::as_error("String is nullptr!");
//     } else if (strlen(string) == 0) {
//       return fit::as_error("String is empty!");
//     }
//     return fit::ok(string);
//   }
template <typename T>
constexpr success<std::decay_t<T>> ok(T&& value) {
  return success<std::decay_t<T>>(std::forward<T>(value));
}

// Overload for empty value success.
constexpr success<> ok() { return success<>{}; }

// Result type representing either an error or zero/one return values.
//
// Base type.
template <typename E, typename... Ts>
class result;

// This suppresses the '-Wctad-maybe-unsupported' compiler warning when CTAD is used.
//
// See https://github.com/llvm/llvm-project/blob/42874f6/libcxx/include/__config#L1259-L1261.
template <class... Tag>
result(typename Tag::__allow_ctad...) -> result<Tag...>;

// Specialization of result for one value type.
template <typename E, typename T>
class [[nodiscard]] result<E, T> {
  static_assert(!::fit::internal::is_success_v<E>,
                "fit::success may not be used as the error type of fit::result!");
  static_assert(!cpp17::is_same_v<failed, std::decay_t<T>>,
                "fit::failed may not be used as a value type of fit::result!");

  template <typename U>
  using not_same = cpp17::negation<std::is_same<result, U>>;

  struct none {};
  using failed_or_none = std::conditional_t<cpp17::is_same_v<failed, E>, failed, none>;

 public:
  using error_type = E;
  using value_type = T;

  constexpr result(const result&) = default;
  constexpr result& operator=(const result&) = default;
  constexpr result(result&&) = default;
  constexpr result& operator=(result&&) = default;

  // Implicit conversion from fit::failed. This overload is only enabled when the error type E is
  // fit::failed.
  constexpr result(failed_or_none) : storage_{::fit::internal::error_v, failed{}} {}

  // Implicit conversion from success<U>, where T is constructible from U.
  template <typename U, ::fit::internal::requires_conditions<std::is_constructible<T, U>> = true>
  constexpr result(success<U> success)
      : storage_{::fit::internal::value_v, std::move(success.value_)} {}

  // Implicit conversion from error<F>, where E is constructible from F.
  template <typename F, ::fit::internal::requires_conditions<std::is_constructible<E, F>> = true>
  constexpr result(error<F> error) : storage_{::fit::internal::error_v, std::move(error.value_)} {}

  // Implicitly constructs a result from another result with compatible types.
  template <
      typename F, typename U,
      ::fit::internal::requires_conditions<not_same<result<F, U>>, std::is_constructible<E, F>,
                                           std::is_constructible<T, U>> = true>
  constexpr result(result<F, U> other) : storage_{std::move(other.storage_)} {}

  // Predicates indicating whether the result contains a value or an error. The positive values are
  // mutually exclusive, however, both predicates are negative when the result is default
  // constructed to the empty state.
  constexpr bool is_ok() const { return storage_.state == ::fit::internal::state_e::has_value; }
  constexpr bool is_error() const { return storage_.state == ::fit::internal::state_e::has_error; }

  // Accessors for the underlying error.
  //
  // May only be called when the result contains an error.
  constexpr E& error_value() & {
    if (is_error()) {
      return storage_.error_or_value.error;
    }
    PW_ASSERT(false);
  }
  constexpr const E& error_value() const& {
    if (is_error()) {
      return storage_.error_or_value.error;
    }
    PW_ASSERT(false);
  }
  constexpr E&& error_value() && {
    if (is_error()) {
      return std::move(storage_.error_or_value.error);
    }
    PW_ASSERT(false);
  }
  constexpr const E&& error_value() const&& {
    if (is_error()) {
      return std::move(storage_.error_or_value.error);
    }
    PW_ASSERT(false);
  }

  // Moves the underlying error and returns it as an instance of fit::error, simplifying
  // propagating the error to another fit::result.
  //
  // May only be called when the result contains an error.
  constexpr error<E> take_error() {
    if (is_error()) {
      return error<E>(std::move(storage_.error_or_value.error));
    }
    PW_ASSERT(false);
  }

  // Accessors for the underlying value.
  //
  // May only be called when the result contains a value.
  constexpr T& value() & {
    if (is_ok()) {
      return storage_.error_or_value.value;
    }
    PW_ASSERT(false);
  }
  constexpr const T& value() const& {
    if (is_ok()) {
      return storage_.error_or_value.value;
    }
    PW_ASSERT(false);
  }
  constexpr T&& value() && {
    if (is_ok()) {
      return std::move(storage_.error_or_value.value);
    }
    PW_ASSERT(false);
  }
  constexpr const T&& value() const&& {
    if (is_ok()) {
      return std::move(storage_.error_or_value.value);
    }
    PW_ASSERT(false);
  }

  // Moves the underlying value and returns it as an instance of fit::success, simplifying
  // propagating the value to another fit::result.
  //
  // May only be called when the result contains a value.
  constexpr success<T> take_value() {
    if (is_ok()) {
      return success<T>(std::move(storage_.error_or_value.value));
    }
    PW_ASSERT(false);
  }

  // Contingent accessors for the underlying value.
  //
  // Returns the value when the result has a value, otherwise returns the given default value.
  template <typename U, ::fit::internal::requires_conditions<std::is_constructible<T, U>> = true>
  constexpr T value_or(U&& default_value) const& {
    if (is_ok()) {
      return storage_.error_or_value.value;
    }
    return static_cast<T>(std::forward<U>(default_value));
  }
  template <typename U, ::fit::internal::requires_conditions<std::is_constructible<T, U>> = true>
  constexpr T value_or(U&& default_value) && {
    if (is_ok()) {
      return std::move(storage_.error_or_value.value);
    }
    return static_cast<T>(std::forward<U>(default_value));
  }

  // Accessors for the members of the underlying value. These operators forward to T::operator->()
  // when defined, otherwise they provide direct access to T*.
  //
  // May only be called when the result contains a value.
  constexpr decltype(auto) operator->() {
    if (is_ok()) {
      return ::fit::internal::arrow_operator<T>::forward(storage_.error_or_value.value);
    }
    PW_ASSERT(false);
  }
  constexpr decltype(auto) operator->() const {
    if (is_ok()) {
      return ::fit::internal::arrow_operator<T>::forward(storage_.error_or_value.value);
    }
    PW_ASSERT(false);
  }

  // Accessors for the underlying value. This is a syntax sugar for value().
  //
  // May only be called when the result contains a value.
  constexpr T& operator*() & { return value(); }
  constexpr const T& operator*() const& { return value(); }
  constexpr T&& operator*() && { return std::move(value()); }
  constexpr const T&& operator*() const&& { return std::move(value()); }

  // Augments the error value of the result with the given error value. The operator
  // E::operator+=(F) must be defined. Additionally, E may not be a pointer, primitive, or enum
  // type.
  //
  // May only be called when the result contains an error.
  template <typename F, ::fit::internal::requires_conditions<
                            std::is_class<E>, ::fit::internal::has_plus_equals<E, F>> = true>
  constexpr result& operator+=(error<F> error) {
    if (is_error()) {
      storage_.error_or_value.error += std::move(error.value_);
      return *this;
    }
    PW_ASSERT(false);
  }

  // Maps a result<E, T> to a result<E2, T> by transforming the error through
  // fn, where E2 is the result of invoking fn on E. Success values will be
  // passed through untouched.
  //
  // Note that map_error is not necessary if E2 is constructible from E.
  // In that case, result<E2, T> will be constructible from result<E, T>.
  //
  // If the current object is an r-value, errors and successes in the current
  // result object will be moved.
  template <typename Fn>
  constexpr result<std::invoke_result_t<Fn, E>, T> map_error(Fn&& fn) & {
    if (is_error()) {
      return error<std::invoke_result_t<Fn, E>>(std::forward<Fn>(fn)(error_value()));
    }
    return success<T>(value());
  }
  template <typename Fn>
  constexpr result<std::invoke_result_t<Fn, E>, T> map_error(Fn&& fn) const& {
    if (is_error()) {
      return error<std::invoke_result_t<Fn, E>>(std::forward<Fn>(fn)(error_value()));
    }
    return success<T>(value());
  }
  template <typename Fn>
  constexpr result<std::invoke_result_t<Fn, E>, T> map_error(Fn&& fn) && {
    if (is_error()) {
      return error<std::invoke_result_t<Fn, E>>(std::forward<Fn>(fn)(std::move(error_value())));
    }
    return take_value();
  }
  template <typename Fn>
  constexpr result<std::invoke_result_t<Fn, E>, T> map_error(Fn&& fn) const&& {
    if (is_error()) {
      return error<std::invoke_result_t<Fn, E>>(std::forward<Fn>(fn)(std::move(error_value())));
    }
    return success<T>(value());
  }

  constexpr void swap(result& other) {
    if (&other != this) {
      using std::swap;
      swap(storage_, other.storage_);
    }
  }

 protected:
  // Default constructs a result in empty state.
  constexpr result() = default;

  // Reset is not a recommended operation for the general result pattern. This method is provided
  // for derived types that need it for specific use cases.
  constexpr void reset() { storage_.reset(); }

 private:
  template <typename, typename...>
  friend class result;

  ::fit::internal::storage<E, T> storage_;
};

// Specialization of the result type for zero values.
template <typename E>
class [[nodiscard]] result<E> {
  static_assert(!::fit::internal::is_success_v<E>,
                "fit::success may not be used as the error type of fit::result!");

  template <typename U>
  using not_same = cpp17::negation<std::is_same<result, U>>;

  template <size_t>
  struct none {};
  using failure_or_none = std::conditional_t<cpp17::is_same_v<failed, E>, failed, none<1>>;

 public:
  using error_type = E;

  constexpr result(const result&) = default;
  constexpr result& operator=(const result&) = default;
  constexpr result(result&&) = default;
  constexpr result& operator=(result&&) = default;

  // Implicit conversion from fit::failure. This overload is only enabled when the error type E is
  // fit::failed.
  constexpr result(failure_or_none) : storage_{::fit::internal::error_v, failed{}} {}

  // Implicit conversion from fit::success<>.
  constexpr result(success<>) : storage_{::fit::internal::value_v} {}

  // Implicit conversion from error<F>, where E is constructible from F.
  template <typename F, ::fit::internal::requires_conditions<std::is_constructible<E, F>> = true>
  constexpr result(error<F> error) : storage_{::fit::internal::error_v, std::move(error.value_)} {}

  // Implicitly constructs a result from another result with compatible types.
  template <typename F, ::fit::internal::requires_conditions<not_same<result<F>>,
                                                             std::is_constructible<E, F>> = true>
  constexpr result(result<F> other) : storage_{std::move(other.storage_)} {}

  // Predicates indicating whether the result contains a value or an error.
  constexpr bool is_ok() const { return storage_.state == ::fit::internal::state_e::has_value; }
  constexpr bool is_error() const { return storage_.state == ::fit::internal::state_e::has_error; }

  // Accessors for the underlying error.
  //
  // May only be called when the result contains an error.
  constexpr E& error_value() & {
    if (is_error()) {
      return storage_.error_or_value.error;
    }
    PW_ASSERT(false);
  }
  constexpr const E& error_value() const& {
    if (is_error()) {
      return storage_.error_or_value.error;
    }
    PW_ASSERT(false);
  }
  constexpr E&& error_value() && {
    if (is_error()) {
      return std::move(storage_.error_or_value.error);
    }
    PW_ASSERT(false);
  }
  constexpr const E&& error_value() const&& {
    if (is_error()) {
      return std::move(storage_.error_or_value.error);
    }
    PW_ASSERT(false);
  }

  // Moves the underlying error and returns it as an instance of fit::error, simplifying
  // propagating the error to another fit::result.
  //
  // May only be called when the result contains an error.
  constexpr error<E> take_error() {
    if (is_error()) {
      return error<E>(std::move(storage_.error_or_value.error));
    }
    PW_ASSERT(false);
  }

  // Augments the error value of the result with the given value. The operator E::operator+=(F) must
  // be defined. Additionally, E may not be a pointer, primitive, or enum type.
  //
  // May only be called when the result contains an error.
  template <typename F, ::fit::internal::requires_conditions<
                            std::is_class<E>, ::fit::internal::has_plus_equals<E, F>> = true>
  constexpr result& operator+=(error<F> error) {
    if (is_error()) {
      storage_.error_or_value.error += std::move(error.value_);
      return *this;
    }
    PW_ASSERT(false);
  }

  // Maps a result<E, T> to a result<E2, T> by transforming the error through
  // fn, where E2 is the result of invoking fn on E. Success values will be
  // passed through untouched.
  //
  // Note that map_error is not necessary if E2 is constructible from E.
  // In that case, result<E2, T> will be constructible from result<E, T>.
  //
  // If the current object is an r-value, errors in the current result object
  // will be moved.
  template <typename Fn>
  constexpr result<std::invoke_result_t<Fn, E>> map_error(Fn&& fn) & {
    if (is_error()) {
      return error<std::invoke_result_t<Fn, E>>(std::forward<Fn>(fn)(error_value()));
    }
    return success<>();
  }
  template <typename Fn>
  constexpr result<std::invoke_result_t<Fn, E>> map_error(Fn&& fn) const& {
    if (is_error()) {
      return error<std::invoke_result_t<Fn, E>>(std::forward<Fn>(fn)(error_value()));
    }
    return success<>();
  }
  template <typename Fn>
  constexpr result<std::invoke_result_t<Fn, E>> map_error(Fn&& fn) && {
    if (is_error()) {
      return error<std::invoke_result_t<Fn, E>>(std::forward<Fn>(fn)(std::move(error_value())));
    }
    return success<>();
  }
  template <typename Fn>
  constexpr result<std::invoke_result_t<Fn, E>> map_error(Fn&& fn) const&& {
    if (is_error()) {
      return error<std::invoke_result_t<Fn, E>>(std::forward<Fn>(fn)(std::move(error_value())));
    }
    return success<>();
  }

  constexpr void swap(result& other) {
    if (&other != this) {
      using std::swap;
      swap(storage_, other.storage_);
    }
  }

 protected:
  // Default constructs a result in empty state.
  constexpr result() = default;

  // Reset is not a recommended operation for the general result pattern. This method is provided
  // for derived types that need it for specific use cases.
  constexpr void reset() { storage_.reset(); }

 private:
  template <typename, typename...>
  friend class result;

  ::fit::internal::storage<E> storage_;
};

template <typename E, typename... Ts>
constexpr void swap(result<E, Ts...>& r, result<E, Ts...>& s) {
  return r.swap(s);
}

// Relational Operators.
//
// Results are comparable to the follownig types:
//  * Other results with the same arity when the value types are comparable.
//  * Any type that is comparable to the value type when the arity is 1.
//  * Any instance of fit::success<> (i.e. fit::ok()).
//  * Any instance of fit::failed.
//
// Result comparisons behave similarly to std::optional<T>, having the same empty and non-empty
// lexicographic ordering. A non-value result behaves like an empty std::optional, regardless of the
// value of the actual error. Error values are never compared, only the is_ok() predicate and result
// values are considered in comparisons.

// Equal/not equal to fit::success.
template <typename E, typename... Ts>
constexpr bool operator==(const result<E, Ts...>& lhs, const success<>&) {
  return lhs.is_ok();
}
template <typename E, typename... Ts>
constexpr bool operator!=(const result<E, Ts...>& lhs, const success<>&) {
  return !lhs.is_ok();
}

template <typename E, typename... Ts>
constexpr bool operator==(const success<>&, const result<E, Ts...>& rhs) {
  return rhs.is_ok();
}
template <typename E, typename... Ts>
constexpr bool operator!=(const success<>&, const result<E, Ts...>& rhs) {
  return !rhs.is_ok();
}

// Equal/not equal to fit::failed.
template <typename E, typename... Ts>
constexpr bool operator==(const result<E, Ts...>& lhs, failed) {
  return lhs.is_error();
}
template <typename E, typename... Ts>
constexpr bool operator!=(const result<E, Ts...>& lhs, failed) {
  return !lhs.is_error();
}

template <typename E, typename... Ts>
constexpr bool operator==(failed, const result<E, Ts...>& rhs) {
  return rhs.is_error();
}
template <typename E, typename... Ts>
constexpr bool operator!=(failed, const result<E, Ts...>& rhs) {
  return !rhs.is_error();
}

// Equal/not equal.
template <typename E, typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() == std::declval<U>())> = true>
constexpr bool operator==(const result<E, T>& lhs, const result<F, U>& rhs) {
  return (lhs.is_ok() == rhs.is_ok()) && (!lhs.is_ok() || lhs.value() == rhs.value());
}
template <typename E, typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() != std::declval<U>())> = true>
constexpr bool operator!=(const result<E, T>& lhs, const result<F, U>& rhs) {
  return (lhs.is_ok() != rhs.is_ok()) || (lhs.is_ok() && lhs.value() != rhs.value());
}

template <typename E, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() == std::declval<U>()),
                                         ::fit::internal::not_result_type<U>> = true>
constexpr bool operator==(const result<E, T>& lhs, const U& rhs) {
  return lhs.is_ok() && lhs.value() == rhs;
}
template <typename E, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() != std::declval<U>()),
                                         ::fit::internal::not_result_type<U>> = true>
constexpr bool operator!=(const result<E, T>& lhs, const U& rhs) {
  return !lhs.is_ok() || lhs.value() != rhs;
}

template <typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() == std::declval<U>()),
                                         ::fit::internal::not_result_type<T>> = true>
constexpr bool operator==(const T& lhs, const result<F, U>& rhs) {
  return rhs.is_ok() && lhs == rhs.value();
}
template <typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() != std::declval<U>()),
                                         ::fit::internal::not_result_type<T>> = true>
constexpr bool operator!=(const T& lhs, const result<F, U>& rhs) {
  return !rhs.is_ok() || lhs != rhs.value();
}

// Less than/greater than.
template <typename E, typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() < std::declval<U>())> = true>
constexpr bool operator<(const result<E, T>& lhs, const result<F, U>& rhs) {
  return rhs.is_ok() && (!lhs.is_ok() || lhs.value() < rhs.value());
}
template <typename E, typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() > std::declval<U>())> = true>
constexpr bool operator>(const result<E, T>& lhs, const result<F, U>& rhs) {
  return lhs.is_ok() && (!rhs.is_ok() || lhs.value() > rhs.value());
}

template <typename E, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() < std::declval<U>()),
                                         ::fit::internal::not_result_type<U>> = true>
constexpr bool operator<(const result<E, T>& lhs, const U& rhs) {
  return !lhs.is_ok() || lhs.value() < rhs;
}
template <typename E, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() > std::declval<U>()),
                                         ::fit::internal::not_result_type<U>> = true>
constexpr bool operator>(const result<E, T>& lhs, const U& rhs) {
  return lhs.is_ok() && lhs.value() > rhs;
}

template <typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() < std::declval<U>()),
                                         ::fit::internal::not_result_type<T>> = true>
constexpr bool operator<(const T& lhs, const result<F, U>& rhs) {
  return rhs.is_ok() && lhs < rhs.value();
}
template <typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() > std::declval<U>()),
                                         ::fit::internal::not_result_type<T>> = true>
constexpr bool operator>(const T& lhs, const result<F, U>& rhs) {
  return !rhs.is_ok() || lhs > rhs.value();
}

// Less than or equal/greater than or equal.
template <typename E, typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() <= std::declval<U>())> = true>
constexpr bool operator<=(const result<E, T>& lhs, const result<F, U>& rhs) {
  return !lhs.is_ok() || (rhs.is_ok() && lhs.value() <= rhs.value());
}
template <typename E, typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() >= std::declval<U>())> = true>
constexpr bool operator>=(const result<E, T>& lhs, const result<F, U>& rhs) {
  return !rhs.is_ok() || (lhs.is_ok() && lhs.value() >= rhs.value());
}

template <typename E, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() <= std::declval<U>()),
                                         ::fit::internal::not_result_type<U>> = true>
constexpr bool operator<=(const result<E, T>& lhs, const U& rhs) {
  return !lhs.is_ok() || lhs.value() <= rhs;
}
template <typename E, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() >= std::declval<U>()),
                                         ::fit::internal::not_result_type<U>> = true>
constexpr bool operator>=(const result<E, T>& lhs, const U& rhs) {
  return lhs.is_ok() && lhs.value() >= rhs;
}

template <typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() <= std::declval<U>()),
                                         ::fit::internal::not_result_type<T>> = true>
constexpr bool operator<=(const T& lhs, const result<F, U>& rhs) {
  return rhs.is_ok() && lhs <= rhs.value();
}
template <typename F, typename T, typename U,
          ::fit::internal::enable_rel_op<decltype(std::declval<T>() >= std::declval<U>()),
                                         ::fit::internal::not_result_type<T>> = true>
constexpr bool operator>=(const T& lhs, const result<F, U>& rhs) {
  return !rhs.is_ok() || lhs >= rhs.value();
}

}  // namespace fit

#endif  // LIB_FIT_RESULT_H_