-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_option_result_base.hpp
4991 lines (4324 loc) · 196 KB
/
_option_result_base.hpp
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// _option_result_base.hpp
#pragma once
#include "_include.hpp"
#include "_detail.hpp"
#include "panic.hpp"
#include <cassert>
#include <exception>
#include <functional>
#include <iostream>
#include <new>
#include <string_view>
#include <type_traits>
#include <utility>
namespace rust {
namespace option {
template<class T>
class Option;
namespace { struct do_not_use{}; }
struct None_t { constexpr explicit None_t(do_not_use, do_not_use) noexcept {} };
static constexpr None_t None{do_not_use{}, do_not_use{}};
} // namespace option
namespace result {
template<class T, class E>
class Result;
struct err_tag_t { err_tag_t() = default; };
static constexpr err_tag_t err_tag{};
struct ok_tag_t { ok_tag_t() = default; };
static constexpr ok_tag_t ok_tag{};
// ------------------------------------------------------------------------------------------
// Result
namespace detail {
// https://stackoverflow.com/questions/26744589/what-is-a-proper-way-to-implement-is-swappable-to-test-for-the-swappable-concept
namespace swap_adl_tests {
// if swap ADL finds this then it would call std::swap otherwise (same
// signature)
struct tag {};
template <class T> tag swap(T&, T&);
template <class T, std::size_t N> tag swap(T(&a)[N], T(&b)[N]);
// helper functions to test if an unqualified swap is possible, and if it
// becomes std::swap
template <class, class> std::false_type can_swap(...) noexcept(false);
template <class T, class U,
class = decltype(swap(std::declval<T&>(), std::declval<U&>()))>
std::true_type can_swap(int) noexcept(noexcept(swap(std::declval<T&>(),
std::declval<U&>())));
template <class, class> std::false_type uses_std(...);
template <class T, class U>
std::is_same<decltype(swap(std::declval<T&>(), std::declval<U&>())), tag>
uses_std(int);
template <class T>
struct is_std_swap_noexcept
: std::integral_constant<bool,
std::is_nothrow_move_constructible<T>::value&&
std::is_nothrow_move_assignable<T>::value> {};
template <class T, std::size_t N>
struct is_std_swap_noexcept<T[N]> : is_std_swap_noexcept<T> {};
template <class T, class U>
struct is_adl_swap_noexcept
: std::integral_constant<bool, noexcept(can_swap<T, U>(0))> {};
} // namespace swap_adl_tests
template <class T, class U = T>
struct is_swappable
: std::integral_constant<bool,
decltype(detail::swap_adl_tests::can_swap<T, U>(0))::value &&
(!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value ||
(std::is_move_assignable<T>::value &&
std::is_move_constructible<T>::value))> {};
template <class T, std::size_t N>
struct is_swappable<T[N], T[N]>
: std::integral_constant<bool,
decltype(detail::swap_adl_tests::can_swap<T[N], T[N]>(0))::value &&
(!decltype(detail::swap_adl_tests::uses_std<T[N], T[N]>(0))::value ||
is_swappable<T, T>::value)> {};
template <class T, class U = T>
struct is_nothrow_swappable
: std::integral_constant<bool,
is_swappable<T, U>::value &&
((decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value &&
detail::swap_adl_tests::is_std_swap_noexcept<T>::value) ||
(!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value &&
detail::swap_adl_tests::is_adl_swap_noexcept<T,U>::value))> {};
template <class T, class E, class U>
using Result_enable_forward_value = std::enable_if_t<
std::is_constructible_v<T, U&&> &&
!std::is_same_v<std::decay_t<U>, ok_tag_t> &&
!std::is_same_v<std::decay_t<U>, err_tag_t> &&
!std::is_same_v<std::decay_t<U>, Result<T, E>>>;
template <class T, class E, class U, class G, class UR, class GR>
using Result_enable_from_other = std::enable_if_t<
std::is_constructible_v<T, UR> &&
std::is_constructible_v<E, GR> &&
!std::is_constructible_v<T, Result<U, G>&> &&
!std::is_constructible_v<T, Result<U, G>&&> &&
!std::is_constructible_v<T, Result<U, G> const&> &&
!std::is_constructible_v<T, Result<U, G> const&&> &&
!std::is_convertible_v<Result<U, G>&, T> &&
!std::is_convertible_v<Result<U, G>&&, T> &&
!std::is_convertible_v<Result<U, G> const&, T> &&
!std::is_convertible_v<Result<U, G> const&&, T>>;
template <class T, class U>
using is_void_or = std::conditional_t<std::is_void_v<T>, std::true_type, U>;
template <class T>
using is_copy_constructible_or_void = is_void_or<T, std::is_copy_constructible<T>>;
template <class T>
using is_move_constructible_or_void = is_void_or<T, std::is_move_constructible<T>>;
template <class T>
using is_copy_assignable_or_void = is_void_or<T, std::is_copy_assignable<T>>;
template <class T>
using is_move_assignable_or_void = is_void_or<T, std::is_move_assignable<T>>;
} // namespace detail
namespace detail {
struct no_init_t {};
static constexpr no_init_t no_init{};
// Implements the storage of the values, and ensures that the destructor is
// trivial if it can be.
//
// This specialization is for where neither `T` or `E` is trivially
// destructible, so the destructors must be called on destruction of the
// `Result`
template <class T, class E, bool = std::is_trivially_destructible_v<T>, bool = std::is_trivially_destructible_v<E>>
struct Result_storage_base {
constexpr Result_storage_base()
: value_(T{})
, is_ok_(true)
{}
constexpr Result_storage_base(no_init_t)
: no_init_()
, is_ok_(false)
{}
template <class... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
constexpr Result_storage_base(ok_tag_t, Args&&... args)
: value_(std::forward<Args>(args)...)
, is_ok_(true)
{}
template <class U, class... Args, std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* = nullptr>
constexpr Result_storage_base(ok_tag_t, std::initializer_list<U> il, Args&&... args)
: value_(il, std::forward<Args>(args)...)
, is_ok_(true)
{}
template <class... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
constexpr explicit Result_storage_base(err_tag_t, Args&&... args)
: err_(std::forward<Args>(args)...)
, is_ok_(false)
{}
template <class U, class... Args, std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>>* = nullptr>
constexpr explicit Result_storage_base(err_tag_t, std::initializer_list<U> il, Args&&... args)
: err_(il, std::forward<Args>(args)...)
, is_ok_(false)
{}
~Result_storage_base() {
if (is_ok_)
value_.~T();
else
err_.~E();
}
union {
T value_;
E err_;
char no_init_;
};
bool is_ok_;
};
// This specialization is for when both `T` and `E` are trivially-destructible,
// so the destructor of the `Result` can be trivial.
template<class T, class E>
struct Result_storage_base<T, E, true, true> {
constexpr Result_storage_base()
: value_(T{})
, is_ok_(true)
{}
constexpr Result_storage_base(no_init_t)
: no_init_()
, is_ok_(false)
{}
template<class... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
constexpr Result_storage_base(ok_tag_t, Args&&... args)
: value_(std::forward<Args>(args)...)
, is_ok_(true)
{}
template<class U, class... Args, std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* = nullptr>
constexpr Result_storage_base(ok_tag_t, std::initializer_list<U> il, Args&&... args)
: value_(il, std::forward<Args>(args)...)
, is_ok_(true)
{}
template<class... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
constexpr explicit Result_storage_base(err_tag_t, Args&&... args)
: err_(std::forward<Args>(args)...)
, is_ok_(false)
{}
template<class U, class... Args, std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>>* = nullptr>
constexpr explicit Result_storage_base(err_tag_t, std::initializer_list<U> il, Args&&... args)
: err_(il, std::forward<Args>(args)...)
, is_ok_(false)
{}
~Result_storage_base() = default;
union {
T value_;
E err_;
char no_init_;
};
bool is_ok_;
};
// T is trivial, E is not.
template<class T, class E>
struct Result_storage_base<T, E, true, false> {
constexpr Result_storage_base()
: value_(T{})
, is_ok_(true)
{}
constexpr Result_storage_base(no_init_t)
: no_init_()
, is_ok_(false)
{}
template<class... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
constexpr Result_storage_base(ok_tag_t, Args&&... args)
: value_(std::forward<Args>(args)...)
, is_ok_(true)
{}
template<class U, class... Args, std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* = nullptr>
constexpr Result_storage_base(ok_tag_t, std::initializer_list<U> il, Args&&... args)
: value_(il, std::forward<Args>(args)...)
, is_ok_(true)
{}
template<class... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
constexpr explicit Result_storage_base(err_tag_t, Args&&... args)
: err_(std::forward<Args>(args)...)
, is_ok_(false)
{}
template<class U, class... Args, std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U> &, Args &&...>>* = nullptr>
constexpr explicit Result_storage_base(err_tag_t, std::initializer_list<U> il, Args &&... args)
: err_(il, std::forward<Args>(args)...)
, is_ok_(false)
{}
~Result_storage_base() {
if (!is_ok_)
err_.~E();
}
union {
T value_;
E err_;
char no_init_;
};
bool is_ok_;
};
// E is trivial, T is not.
template<class T, class E>
struct Result_storage_base<T, E, false, true> {
constexpr Result_storage_base()
: value_(T{})
, is_ok_(true)
{}
constexpr Result_storage_base(no_init_t)
: no_init_()
, is_ok_(false)
{}
template<class... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
constexpr Result_storage_base(ok_tag_t, Args&&... args)
: value_(std::forward<Args>(args)...)
, is_ok_(true)
{}
template<class U, class... Args, std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* = nullptr>
constexpr Result_storage_base(ok_tag_t, std::initializer_list<U> il, Args&&... args)
: value_(il, std::forward<Args>(args)...)
, is_ok_(true)
{}
template<class... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
constexpr explicit Result_storage_base(err_tag_t, Args&&... args)
: err_(std::forward<Args>(args)...)
, is_ok_(false)
{}
template <class U, class... Args, std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>> * = nullptr>
constexpr explicit Result_storage_base(err_tag_t, std::initializer_list<U> il, Args&&... args)
: err_(il, std::forward<Args>(args)...)
, is_ok_(false)
{}
~Result_storage_base() {
if (is_ok_)
value_.~T();
}
union {
T value_;
E err_;
char no_init_;
};
bool is_ok_;
};
// This base class provides some handy member functions which can be used in
// further derived classes
template <class T, class E>
struct Result_operations_base : Result_storage_base<T, E> {
using Result_storage_base<T, E>::Result_storage_base;
template <class... Args>
void construct(Args&&... args) noexcept {
new (std::addressof(this->value_)) T(std::forward<Args>(args)...);
this->is_ok_ = true;
}
template <class Rhs>
void construct_with(Rhs&& rhs) noexcept {
new (std::addressof(this->value_)) T(std::forward<Rhs>(rhs).get());
this->is_ok_ = true;
}
template <class... Args>
void construct_err(Args&&... args) noexcept {
new (std::addressof(this->err_)) E(std::forward<Args>(args)...);
this->is_ok_ = false;
}
#ifdef RUST_EXCEPTIONS_ENABLED
// These assign overloads ensure that the most efficient assignment
// implementation is used while maintaining the strong exception guarantee.
// The problematic case is where rhs has a value, but *this does not.
//
// This overload handles the case where we can just copy-construct `T`
// directly into place without throwing.
template <class U = T, std::enable_if_t<std::is_nothrow_copy_constructible_v<U>>* = nullptr>
void assign(Result_operations_base const& rhs) noexcept {
if (!this->is_ok_ && rhs.is_ok_) {
geterr().~E();
construct(rhs.get());
}
else
assign_common(rhs);
}
// This overload handles the case where we can attempt to create a copy of
// `T`, then no-throw move it into place if the copy was successful.
template <class U = T, std::enable_if_t<!std::is_nothrow_copy_constructible_v<U> && std::is_nothrow_move_constructible_v<U>>* = nullptr>
void assign(const Result_operations_base &rhs) noexcept {
if (!this->is_ok_ && rhs.is_ok_) {
T tmp = rhs.get();
geterr().~E();
construct(std::move(tmp));
}
else
assign_common(rhs);
}
// This overload is the worst-case, where we have to move-construct the
// err value into temporary storage, then try to copy the T into place.
// If the construction succeeds, then everything is fine, but if it throws,
// then we move the old err value back into place before rethrowing the
// exception.
template <class U = T, std::enable_if_t<!std::is_nothrow_copy_constructible_v<U> && !std::is_nothrow_move_constructible_v<U>>* = nullptr>
void assign(const Result_operations_base &rhs) {
if (!this->is_ok_ && rhs.is_ok_) {
auto tmp = std::move(geterr());
geterr().~E();
#ifdef RUST_EXCEPTIONS_ENABLED
try {
construct(rhs.get());
}
catch (...) {
geterr() = std::move(tmp);
throw;
}
#else // RUST_EXCEPTIONS_ENABLED
construct(rhs.get());
#endif // RUST_EXCEPTIONS_ENABLED
}
else
assign_common(rhs);
}
// These overloads do the same as above, but for rvalues
template <class U = T, std::enable_if_t<std::is_nothrow_move_constructible_v<U>>* = nullptr>
void assign(Result_operations_base&& rhs) noexcept {
if (!this->is_ok_ && rhs.is_ok_) {
geterr().~E();
construct(std::move(rhs).get());
}
else
assign_common(std::move(rhs));
}
template <class U = T, std::enable_if_t<!std::is_nothrow_move_constructible_v<U>>* = nullptr>
void assign(Result_operations_base &&rhs) {
if (!this->is_ok_ && rhs.is_ok_) {
auto tmp = std::move(geterr());
geterr().~E();
#ifdef RUST_EXCEPTIONS_ENABLED
try {
construct(std::move(rhs).get());
}
catch (...) {
geterr() = std::move(tmp);
throw;
}
#else // RUST_EXCEPTIONS_ENABLED
construct(std::move(rhs).get());
#endif // RUST_EXCEPTIONS_ENABLED
}
else
assign_common(std::move(rhs));
}
#else // ^^^RUST_EXCEPTIONS_ENABLED^^^
void assign(const Result_operations_base &rhs) noexcept {
if (!this->is_ok_ && rhs.is_ok_) {
geterr().~E();
construct(rhs.get());
}
else
assign_common(rhs);
}
void assign(Result_operations_base &&rhs) noexcept {
if (!this->is_ok_ && rhs.is_ok_) {
geterr().~E();
construct(std::move(rhs).get());
}
else
assign_common(rhs);
}
#endif // RUST_EXCEPTIONS_ENABLED
// The common part of move/copy assigning
template<class Rhs>
void assign_common(Rhs &&rhs) {
if (this->is_ok_) {
if (rhs.is_ok_)
get() = std::forward<Rhs>(rhs).get();
else {
destroy_val();
construct_err(std::forward<Rhs>(rhs).geterr());
}
}
else {
if (!rhs.is_ok_)
geterr() = std::forward<Rhs>(rhs).geterr();
}
}
constexpr bool is_ok() const noexcept { return this->is_ok_; }
constexpr T& get() & { return this->value_; }
constexpr T const& get() const& { return this->value_; }
constexpr T&& get() && { return std::move(this->value_); }
constexpr T const&& get() const&& { return std::move(this->value_); }
constexpr E& geterr() & { return this->err_; }
constexpr E const& geterr() const& { return this->err_; }
constexpr E&& geterr() && { return std::move(this->err_); }
constexpr E const&& geterr() const&& { return std::move(this->err_); }
constexpr void destroy_val() {
get().~T();
}
};
// This base class provides some handy member functions which can be used in
// further derived classes
// This class manages conditionally having a trivial copy constructor
// This specialization is for when T and E are trivially copy constructible
template<class T, class E, bool = is_void_or<T, std::is_trivially_copy_constructible<T>>::value && std::is_trivially_copy_constructible_v<E>>
struct Result_copy_base : Result_operations_base<T, E> {
using Result_operations_base<T, E>::Result_operations_base;
};
// This specialization is for when T or E are not trivially copy constructible
template<class T, class E>
struct Result_copy_base<T, E, false> : Result_operations_base<T, E> {
using Result_operations_base<T, E>::Result_operations_base;
Result_copy_base() = default;
Result_copy_base(const Result_copy_base &rhs)
: Result_operations_base<T, E>(no_init)
{
if (rhs.is_ok())
this->construct_with(rhs);
else
this->construct_err(rhs.geterr());
}
Result_copy_base(Result_copy_base &&rhs) = default;
Result_copy_base &operator=(const Result_copy_base &rhs) = default;
Result_copy_base &operator=(Result_copy_base &&rhs) = default;
};
// This class manages conditionally having a trivial move constructor
template<class T, class E, bool = is_void_or<T, std::is_trivially_move_constructible<T>>::value && std::is_trivially_move_constructible_v<E>>
struct Result_move_base : Result_copy_base<T, E> {
using Result_copy_base<T, E>::Result_copy_base;
};
template <class T, class E>
struct Result_move_base<T, E, false> : Result_copy_base<T, E> {
using Result_copy_base<T, E>::Result_copy_base;
Result_move_base() = default;
Result_move_base(const Result_move_base &rhs) = default;
Result_move_base(Result_move_base &&rhs)
noexcept(std::is_nothrow_move_constructible<T>::value)
: Result_copy_base<T, E>(no_init)
{
if (rhs.is_ok())
this->construct_with(std::move(rhs));
else
this->construct_err(std::move(rhs.geterr()));
}
Result_move_base &operator=(const Result_move_base &rhs) = default;
Result_move_base &operator=(Result_move_base &&rhs) = default;
};
// This class manages conditionally having a trivial copy assignment operator
template <class T, class E, bool =
is_void_or<T, std::conjunction<std::is_trivially_copy_assignable<T>, std::is_trivially_copy_constructible<T>, std::is_trivially_destructible<T>>>::value
&& std::is_trivially_copy_assignable_v<E> && std::is_trivially_copy_constructible_v<E> && std::is_trivially_destructible_v<E>>
struct Result_copy_assign_base : Result_move_base<T, E> {
using Result_move_base<T, E>::Result_move_base;
};
template <class T, class E>
struct Result_copy_assign_base<T, E, false> : Result_move_base<T, E> {
using Result_move_base<T, E>::Result_move_base;
Result_copy_assign_base() = default;
Result_copy_assign_base(Result_copy_assign_base const& rhs) = default;
Result_copy_assign_base(Result_copy_assign_base&& rhs) = default;
Result_copy_assign_base &operator=(Result_copy_assign_base const& rhs) {
this->assign(rhs);
return *this;
}
Result_copy_assign_base& operator=(Result_copy_assign_base&& rhs) = default;
};
// This class manages conditionally having a trivial move assignment operator
template<class T, class E, bool =
is_void_or<T, std::conjunction<std::is_trivially_destructible<T>, std::is_trivially_move_constructible<T>, std::is_trivially_move_assignable<T>>>::value
&& std::is_trivially_destructible_v<E> && std::is_trivially_move_constructible_v<E> && std::is_trivially_move_assignable_v<E>>
struct Result_move_assign_base : Result_copy_assign_base<T, E> {
using Result_copy_assign_base<T, E>::Result_copy_assign_base;
};
template<class T, class E>
struct Result_move_assign_base<T, E, false> : Result_copy_assign_base<T, E> {
using Result_copy_assign_base<T, E>::Result_copy_assign_base;
Result_move_assign_base() = default;
Result_move_assign_base(Result_move_assign_base const& rhs) = default;
Result_move_assign_base(Result_move_assign_base&& rhs) = default;
Result_move_assign_base& operator=(Result_move_assign_base const& rhs) = default;
Result_move_assign_base& operator=(Result_move_assign_base&& rhs)
noexcept(std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_assignable_v<T>) {
this->assign(std::move(rhs));
return *this;
}
};
// Result_delete_ctor_base will conditionally delete copy and move
// constructors depending on whether T is copy/move constructible
template<class T, class E,
bool EnableCopy = (is_copy_constructible_or_void<T>::value &&
std::is_copy_constructible_v<E>),
bool EnableMove = (is_move_constructible_or_void<T>::value &&
std::is_move_constructible_v<E>)>
struct Result_delete_ctor_base {
Result_delete_ctor_base() = default;
Result_delete_ctor_base(Result_delete_ctor_base const&) = default;
Result_delete_ctor_base(Result_delete_ctor_base&&) noexcept = default;
Result_delete_ctor_base& operator=(Result_delete_ctor_base const&) = default;
Result_delete_ctor_base& operator=(Result_delete_ctor_base&&) noexcept = default;
};
template<class T, class E>
struct Result_delete_ctor_base<T, E, true, false> {
Result_delete_ctor_base() = default;
Result_delete_ctor_base(Result_delete_ctor_base const&) = default;
Result_delete_ctor_base(Result_delete_ctor_base &&) noexcept = delete;
Result_delete_ctor_base& operator=(Result_delete_ctor_base const&) = default;
Result_delete_ctor_base& operator=(Result_delete_ctor_base&&) noexcept = default;
};
template<class T, class E>
struct Result_delete_ctor_base<T, E, false, true> {
Result_delete_ctor_base() = default;
Result_delete_ctor_base(Result_delete_ctor_base const&) = delete;
Result_delete_ctor_base(Result_delete_ctor_base&&) noexcept = default;
Result_delete_ctor_base& operator=(Result_delete_ctor_base const&) = default;
Result_delete_ctor_base& operator=(Result_delete_ctor_base&&) noexcept = default;
};
template<class T, class E>
struct Result_delete_ctor_base<T, E, false, false> {
Result_delete_ctor_base() = default;
Result_delete_ctor_base(Result_delete_ctor_base const&) = delete;
Result_delete_ctor_base(Result_delete_ctor_base&&) noexcept = delete;
Result_delete_ctor_base& operator=(Result_delete_ctor_base const&) = default;
Result_delete_ctor_base& operator=(Result_delete_ctor_base&&) noexcept = default;
};
// Result_delete_assign_base will conditionally delete copy and move
// constructors depending on whether T and E are copy/move constructible +
// assignable
template<class T, class E,
bool EnableCopy = (is_copy_constructible_or_void<T>::value &&
std::is_copy_constructible_v<E> &&
is_copy_assignable_or_void<T>::value &&
std::is_copy_assignable_v<E>),
bool EnableMove = (is_move_constructible_or_void<T>::value &&
std::is_move_constructible_v<E> &&
is_move_assignable_or_void<T>::value &&
std::is_move_assignable_v<E>)>
struct Result_delete_assign_base {
Result_delete_assign_base() = default;
Result_delete_assign_base(Result_delete_assign_base const&) = default;
Result_delete_assign_base(Result_delete_assign_base&&) noexcept = default;
Result_delete_assign_base& operator=(Result_delete_assign_base const&) = default;
Result_delete_assign_base& operator=(Result_delete_assign_base&&) noexcept = default;
};
template<class T, class E>
struct Result_delete_assign_base<T, E, true, false> {
Result_delete_assign_base() = default;
Result_delete_assign_base(Result_delete_assign_base const&) = default;
Result_delete_assign_base(Result_delete_assign_base&&) noexcept = default;
Result_delete_assign_base& operator=(Result_delete_assign_base const&) = default;
Result_delete_assign_base& operator=(Result_delete_assign_base&&) noexcept = delete;
};
template<class T, class E>
struct Result_delete_assign_base<T, E, false, true> {
Result_delete_assign_base() = default;
Result_delete_assign_base(Result_delete_assign_base const&) = default;
Result_delete_assign_base(Result_delete_assign_base&&) noexcept = default;
Result_delete_assign_base& operator=(Result_delete_assign_base const&) = delete;
Result_delete_assign_base& operator=(Result_delete_assign_base&&) noexcept = default;
};
template<class T, class E>
struct Result_delete_assign_base<T, E, false, false> {
Result_delete_assign_base() = default;
Result_delete_assign_base(Result_delete_assign_base const&) = default;
Result_delete_assign_base(Result_delete_assign_base&&) noexcept = default;
Result_delete_assign_base& operator=(Result_delete_assign_base const&) = delete;
Result_delete_assign_base& operator=(Result_delete_assign_base&&) noexcept = delete;
};
// This is needed to be able to construct the Result_default_ctor_base which
// follows, while still conditionally deleting the default constructor.
struct default_constructor_tag {
explicit constexpr default_constructor_tag() = default;
};
// Result_default_ctor_base will ensure that Result has a deleted default
// consturctor if T is not default constructible.
// This specialization is for when T is default constructible
template <class T, class E, bool Enable = std::is_default_constructible_v<T> || std::is_void_v<T>>
struct Result_default_ctor_base {
constexpr Result_default_ctor_base() noexcept = default;
constexpr Result_default_ctor_base(Result_default_ctor_base const&) noexcept = default;
constexpr Result_default_ctor_base(Result_default_ctor_base&&) noexcept = default;
Result_default_ctor_base& operator=(Result_default_ctor_base const&) noexcept = default;
Result_default_ctor_base& operator=(Result_default_ctor_base&&) noexcept = default;
constexpr explicit Result_default_ctor_base(default_constructor_tag) {}
};
// This specialization is for when T is not default constructible
template <class T, class E> struct Result_default_ctor_base<T, E, false> {
constexpr Result_default_ctor_base() noexcept = delete;
constexpr Result_default_ctor_base(Result_default_ctor_base const&) noexcept = default;
constexpr Result_default_ctor_base(Result_default_ctor_base&&) noexcept = default;
Result_default_ctor_base& operator=(Result_default_ctor_base const&) noexcept = default;
Result_default_ctor_base& operator=(Result_default_ctor_base&&) noexcept = default;
constexpr explicit Result_default_ctor_base(default_constructor_tag) {}
};
} // namespace detail
template <class T, class E>
class Result : private detail::Result_move_assign_base<T, E>,
private detail::Result_delete_ctor_base<T, E>,
private detail::Result_delete_assign_base<T, E>,
private detail::Result_default_ctor_base<T, E> {
static_assert(!std::is_void_v<T>, "T must not be void");
static_assert(!std::is_void_v<E>, "E must not be void");
static_assert(!std::is_same_v<T, std::remove_cv_t<ok_tag_t>>, "T must not be ok_tag_t");
static_assert(!std::is_same_v<T, std::remove_cv_t<err_tag_t>>, "T must not be err_tag_t");
// val_ptr
T* val_ptr() { return std::addressof(this->value_); }
T const* val_ptr() const { return std::addressof(this->value_); }
// err_ptr
E* err_ptr() { return std::addressof(this->err_); }
E const* err_ptr() const { return std::addressof(this->err_); }
// get_val
constexpr T& get_val() { return this->value_; }
constexpr T const& get_val() const { return this->value_; }
// get_err
constexpr E& get_err() { return this->err_; }
constexpr E const& get_err() const { return this->err_; }
using impl_base = detail::Result_move_assign_base<T, E>;
using ctor_base = detail::Result_default_ctor_base<T, E>;
public:
using ok_type = T;
using err_type = E;
// constructors
constexpr Result() = default;
constexpr Result(Result const& rhs) = default;
constexpr Result(Result&& rhs) = default;
Result& operator=(Result const& rhs) = default;
Result& operator=(Result&& rhs) = default;
template <class... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
constexpr Result(ok_tag_t, Args&&... args)
: impl_base(ok_tag, std::forward<Args>(args)...)
, ctor_base(detail::default_constructor_tag{})
{}
template<class U, class... Args, std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* = nullptr>
constexpr Result(ok_tag_t, std::initializer_list<U> il, Args &&... args)
: impl_base(ok_tag, il, std::forward<Args>(args)...)
, ctor_base(detail::default_constructor_tag{})
{}
template<class... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
constexpr explicit Result(err_tag_t, Args&&... args)
: impl_base(err_tag, std::forward<Args>(args)...)
, ctor_base(detail::default_constructor_tag{})
{}
template <class U, class... Args, std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>>* = nullptr>
constexpr explicit Result(err_tag_t, std::initializer_list<U> il, Args&&... args)
: impl_base(err_tag, il, std::forward<Args>(args)...)
, ctor_base(detail::default_constructor_tag{})
{}
template<class U, class G, std::enable_if_t<!(std::is_convertible_v<U const&, T> && std::is_convertible_v<G const&, E>)>* = nullptr,
detail::Result_enable_from_other<T, E, U, G, U const&, G const&>* = nullptr>
explicit constexpr Result(Result<U, G> const& rhs)
: ctor_base(detail::default_constructor_tag{})
{
if (rhs.is_ok())
this->construct(rhs.unwrap_unsafe());
else
this->construct_err(rhs.unwrap_err_unsafe());
}
template <class U, class G, std::enable_if_t<(std::is_convertible_v<U const&, T> && std::is_convertible_v<G const&, E>)>* = nullptr,
detail::Result_enable_from_other<T, E, U, G, U const&, G const&>* = nullptr>
constexpr Result(Result<U, G> const& rhs)
: ctor_base(detail::default_constructor_tag{})
{
if (rhs.is_ok())
this->construct(rhs.unwrap_unsafe());
else
this->construct_err(rhs.unwrap_err_unsafe());
}
template<class U, class G, std::enable_if_t<!(std::is_convertible_v<U&&, T> && std::is_convertible_v<G&&, E>)>* = nullptr,
detail::Result_enable_from_other<T, E, U, G, U&&, G&&>* = nullptr>
explicit constexpr Result(Result<U, G>&& rhs)
: ctor_base(detail::default_constructor_tag{})
{
if (rhs.is_ok())
this->construct(std::move(rhs.unwrap_unsafe()));
else
this->construct_err(std::move(rhs.unwrap_err_unsafe()));
}
template<class U, class G, std::enable_if_t<(std::is_convertible_v<U&&, T> && std::is_convertible_v<G&&, E>)>* = nullptr,
detail::Result_enable_from_other<T, E, U, G, U&&, G&&>* = nullptr>
constexpr Result(Result<U, G>&& rhs)
: ctor_base(detail::default_constructor_tag{})
{
if (rhs.is_ok())
this->construct(std::move(rhs.unwrap_unsafe()));
else
this->construct_err(std::move(rhs.unwrap_err_unsafe()));
}
template <class U = T, std::enable_if_t<!std::is_convertible_v<U&&, T>>* = nullptr,
detail::Result_enable_forward_value<T, E, U>* = nullptr>
explicit constexpr Result(U&& u)
: Result(ok_tag, std::forward<U>(u))
{}
template <class U = T, std::enable_if_t<std::is_convertible_v<U&&, T>>* = nullptr,
detail::Result_enable_forward_value<T, E, U>* = nullptr>
constexpr Result(U&& u)
: Result(ok_tag, std::forward<U>(u))
{}
// operator=
template <class U = T, class G = T,
std::enable_if_t<std::is_nothrow_constructible_v<T, U&&>>* = nullptr,
std::enable_if_t<!std::is_void_v<G>>* = nullptr,
std::enable_if_t<(!std::is_same_v<Result<T, E>, std::decay_t<U>> &&
!std::conjunction_v<std::is_scalar<T>, std::is_same<T, std::decay_t<U>>> &&
std::is_constructible_v<T, U> &&
std::is_assignable_v<G &, U> &&
std::is_nothrow_move_constructible_v<E>)>* = nullptr>
Result& operator=(U &&u) {
if (is_ok())
get_val() = std::forward<U>(u);
else {
get_err().~E();
::new (val_ptr()) T(std::forward<U>(u));
this->is_ok_ = true;
}
return *this;
}
template<class U = T, class G = T,
std::enable_if_t<!std::is_nothrow_constructible_v<T, U&&>>* = nullptr,
std::enable_if_t<!std::is_void_v<U>>* = nullptr,
std::enable_if_t<(!std::is_same<Result<T, E>, std::decay_t<U>>::value &&
!std::conjunction_v<std::is_scalar<T>, std::is_same<T, std::decay_t<U>>> &&
std::is_constructible_v<T, U> &&
std::is_assignable_v<G&, U> &&
std::is_nothrow_move_constructible_v<E>)>* = nullptr>
Result& operator=(U &&v) {
if (is_ok())
get_val() = std::forward<U>(v);
else {
auto tmp = std::move(get_err());
get_err().~E();
#ifdef RUST_EXCEPTIONS_ENABLED
try {
::new (val_ptr()) T(std::forward<U>(v));
this->is_ok_ = true;
} catch (...) {
get_err() = std::move(tmp);
throw;
}
#else // ^^^RUST_EXCEPTIONS_ENABLED^^^
::new (val_ptr()) T(std::forward<U>(v));
this->is_ok_ = true;
#endif // RUST_EXCEPTIONS_ENABLED
}
return *this;
}
// emplace
template<class... Args, std::enable_if_t<std::is_nothrow_constructible_v<T, Args&&...>>* = nullptr>
T& emplace(Args&&... args) {
if (is_ok())
get_val() = T(std::forward<Args>(args)...);
else {
get_err().~E();
::new (val_ptr()) T(std::forward<Args>(args)...);
this->is_ok_ = true;
}
return get_val();
}
template<class... Args, std::enable_if_t<!std::is_nothrow_constructible_v<T, Args&&...>>* = nullptr>
T& emplace(Args &&... args) {
if (is_ok())
get_val() = T(std::forward<Args>(args)...);
else {
#ifdef RUST_EXCEPTIONS_ENABLED
auto tmp = std::move(get_err());
#endif // ^^^RUST_EXCEPTIONS_ENABLED^^^
get_err().~E();
#ifdef RUST_EXCEPTIONS_ENABLED
try {
::new (val_ptr()) T(std::forward<Args>(args)...);
this->is_ok_ = true;
}
catch (...) {
get_err() = std::move(tmp);
throw;
}
#else // ^^^RUST_EXCEPTIONS_ENABLED^^^
::new (val_ptr()) T(std::forward<Args>(args)...);
this->is_ok_ = true;
#endif // RUST_EXCEPTIONS_ENABLED
}
return get_val();
}
template<class U, class... Args, std::enable_if_t<std::is_nothrow_constructible_v<T, std::initializer_list<U>&, Args&&...>>* = nullptr>
T& emplace(std::initializer_list<U> il, Args&&... args) {
if (is_ok()) {
T t(il, std::forward<Args>(args)...);
get_val() = std::move(t);
}
else {
get_err().~E();
::new (val_ptr()) T(il, std::forward<Args>(args)...);
this->is_ok_ = true;
}
return get_val();
}
template<class U, class... Args, std::enable_if_t<!std::is_nothrow_constructible_v<T, std::initializer_list<U>&, Args&&...>>* = nullptr>
T& emplace(std::initializer_list<U> il, Args &&... args) {
if (is_ok()) {
T t(il, std::forward<Args>(args)...);
get_val() = std::move(t);
}
else {
#ifdef RUST_EXCEPTIONS_ENABLED
auto tmp = std::move(get_err());
#endif // ^^^RUST_EXCEPTIONS_ENABLED^^^
get_err().~E();
#ifdef RUST_EXCEPTIONS_ENABLED
try {
::new (val_ptr()) T(il, std::forward<Args>(args)...);
this->is_ok_ = true;
} catch (...) {
get_err() = std::move(tmp);
throw;
}
#else // ^^^RUST_EXCEPTIONS_ENABLED^^^
::new (val_ptr()) T(il, std::forward<Args>(args)...);
this->is_ok_ = true;
#endif // RUST_EXCEPTIONS_ENABLED
}