-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.hpp
216 lines (198 loc) · 7.63 KB
/
option.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
// Option.hpp
#pragma once
#include "_include.hpp"
#include "_detail.hpp"
#include "_option_result_base.hpp"
#include "panic.hpp"
#include "result.hpp"
#include <functional>
#include <type_traits>
#include <utility>
namespace rust {
namespace option {
// Option<T>::transpose
template<class T>
template<class U>
[[nodiscard]] constexpr std::enable_if_t<rust::detail::is_result_v<U>, result::Result<Option<typename U::ok_type>, typename U::err_type>>
Option<T>::transpose() && {
using ok_t = typename U::ok_type;
using err_t = typename U::err_type;
return bool(*this) ? (bool(**this) ? result::Ok<ok_t, err_t>(std::move((**this).unwrap_unsafe()))
: result::Err<ok_t, err_t>(std::move((**this).unwrap_err_unsafe())))
: result::Ok<ok_t, err_t>(None);
}
template<class T>
template<class U>
[[nodiscard]] constexpr std::enable_if_t<rust::detail::is_result_v<U>, result::Result<Option<typename U::ok_type>, typename U::err_type>>
Option<T&>::transpose() && {
using ok_t = typename U::ok_type;
using err_t = typename U::err_type;
return bool(*this) ? (bool(**this) ? result::Ok<ok_t, err_t>((**this).unwrap_unsafe())
: result::Err<ok_t, err_t>((**this).unwrap_err_unsafe()))
: result::Ok<ok_t, err_t>(None);
}
// Some
template<class T, class... Args>
[[nodiscard]] inline constexpr auto Some(Args&&... args) {
return Option<T>{some_tag, std::forward<Args>(args)...};
}
template<class T, class U, class... Args>
[[nodiscard]] inline constexpr auto Some(std::initializer_list<U> il, Args&&... args) {
return Option<T>{some_tag, il, std::forward<Args>(args)...};
}
/// Compares two Option objects
template <class T, class U>
[[nodiscard]] inline constexpr bool operator==(Option<T> const& lhs, Option<U> const& rhs) {
return bool(lhs) == bool(rhs) && (!bool(lhs) || *lhs == *rhs);
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator!=(Option<T> const& lhs, Option<U> const& rhs) {
return bool(lhs) != bool(rhs) || (bool(lhs) && *lhs != *rhs);
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator<(Option<T> const& lhs, Option<U> const& rhs) {
return bool(rhs) && (!bool(lhs) || *lhs < *rhs);
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator>(Option<T> const& lhs, Option<U> const& rhs) {
return bool(lhs) && (!bool(rhs) || *lhs > * rhs);
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator<=(Option<T> const& lhs, Option<U> const& rhs) {
return !bool(lhs) || (bool(rhs) && *lhs <= *rhs);
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator>=(Option<T> const& lhs, Option<U> const& rhs) {
return !bool(rhs) || (bool(lhs) && *lhs >= *rhs);
}
/// Compares an Option to a `None`
template <class T>
[[nodiscard]] inline constexpr bool operator==(Option<T> const& lhs, None_t) noexcept {
return !bool(lhs);
}
template <class T>
[[nodiscard]] inline constexpr bool operator==(None_t, Option<T> const& rhs) noexcept {
return !bool(rhs);
}
template <class T>
[[nodiscard]] inline constexpr bool operator!=(Option<T> const& lhs, None_t) noexcept {
return bool(lhs);
}
template <class T>
[[nodiscard]] inline constexpr bool operator!=(None_t, Option<T> const& rhs) noexcept {
return bool(rhs);
}
template <class T>
[[nodiscard]] inline constexpr bool operator<(Option<T> const&, None_t) noexcept {
return false;
}
template <class T>
[[nodiscard]] inline constexpr bool operator<(None_t, Option<T> const& rhs) noexcept {
return bool(rhs);
}
template <class T>
[[nodiscard]] inline constexpr bool operator<=(Option<T> const& lhs, None_t) noexcept {
return !bool(lhs);
}
template <class T>
[[nodiscard]] inline constexpr bool operator<=(None_t, Option<T> const&) noexcept {
return true;
}
template <class T>
[[nodiscard]] inline constexpr bool operator>(Option<T> const& lhs, None_t) noexcept {
return bool(lhs);
}
template <class T>
[[nodiscard]] inline constexpr bool operator>(None_t, Option<T> const&) noexcept {
return false;
}
template <class T>
[[nodiscard]] inline constexpr bool operator>=(Option<T> const&, None_t) noexcept {
return true;
}
template <class T>
[[nodiscard]] inline constexpr bool operator>=(None_t, Option<T> const& rhs) noexcept {
return !bool(rhs);
}
/// Compares the Option with a value.
template <class T, class U>
[[nodiscard]] inline constexpr bool operator==(Option<T> const& lhs, U const& rhs) {
return bool(lhs) ? *lhs == rhs : false;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator==(U const& lhs, Option<T> const& rhs) {
return bool(rhs) ? lhs == *rhs : false;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator!=(Option<T> const& lhs, U const& rhs) {
return bool(lhs) ? *lhs != rhs : true;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator!=(U const& lhs, Option<T> const& rhs) {
return bool(rhs) ? lhs != *rhs : true;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator<(Option<T> const& lhs, U const& rhs) {
return bool(lhs) ? *lhs < rhs : true;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator<(U const& lhs, Option<T> const& rhs) {
return bool(rhs) ? lhs < *rhs : false;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator<=(Option<T> const& lhs, U const& rhs) {
return bool(lhs) ? *lhs <= rhs : true;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator<=(U const& lhs, Option<T> const& rhs) {
return bool(rhs) ? lhs <= *rhs : false;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator>(Option<T> const& lhs, U const& rhs) {
return bool(lhs) ? *lhs > rhs : false;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator>(U const& lhs, Option<T> const& rhs) {
return bool(rhs) ? lhs > * rhs : true;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator>=(Option<T> const& lhs, U const& rhs) {
return bool(lhs) ? *lhs >= rhs : false;
}
template <class T, class U>
[[nodiscard]] inline constexpr bool operator>=(U const& lhs, Option<T> const& rhs) {
return bool(rhs) ? lhs >= *rhs : true;
}
template <class T,
std::enable_if_t<std::is_move_constructible_v<T>> * = nullptr,
std::enable_if_t<std::is_swappable_v<T>> * = nullptr>
void swap(Option<T>& lhs, Option<T>& rhs)
noexcept(noexcept(lhs.swap(rhs))) {
return lhs.swap(rhs);
}
} // namesace option
} // namespace rust
namespace {
template <class Key, class Hash>
using _check_hash_requirements = std::bool_constant<
std::is_copy_constructible_v<Hash> &&
std::is_move_constructible_v<Hash> &&
std::is_invocable_r_v<std::size_t, Hash, Key const&>>;
template <class Key, class Hash = std::hash<Key> >
using _has_enabled_hash = std::bool_constant<
_check_hash_requirements<Key, Hash>::value &&
std::is_default_constructible_v<Hash>>;
template <class T, class>
using _enable_hash_helper_impl = T;
template <class T, class Key>
using _enable_hash_helper = _enable_hash_helper_impl<T,
std::enable_if_t<_has_enabled_hash<Key>::value>>;
} // namespace
namespace std {
template<class T>
struct hash<_enable_hash_helper<rust::option::Option<T>, remove_const_t<T>>> {
[[nodiscard]] constexpr size_t operator()(rust::option::Option<T> const& opt) const {
return bool(opt) ? hash<remove_const_t<T>>(*opt) : 0;
}
};
} // namespace std