|
10 | 10 | #include <algorithm> |
11 | 11 | #include <cmath> |
12 | 12 | #include <cstddef> |
| 13 | +#include <expected> |
13 | 14 | #include <sstream> |
14 | 15 | #include <variant> |
15 | 16 | #include <vector> |
@@ -230,69 +231,72 @@ namespace detail { |
230 | 231 | }// namespace detail |
231 | 232 |
|
232 | 233 |
|
233 | | -template<typename T> class PyResult |
| 234 | +template<typename T> class [[nodiscard]] PyResult |
234 | 235 | { |
235 | 236 | public: |
236 | 237 | using OkType = T; |
237 | 238 | using ErrType = BaseException *; |
238 | | - using StorageType = std::variant<Ok<T>, Err>; |
| 239 | + using StorageType = std::expected<T, BaseException *>; |
239 | 240 |
|
240 | 241 | private: |
241 | 242 | StorageType result; |
242 | 243 |
|
243 | 244 | public: |
244 | | - PyResult(Ok<T> result_) : result(std::move(result_)) {} |
245 | | - template<typename U> constexpr PyResult(Ok<U> result_) : result(Ok<T>(std::move(result_.value))) |
| 245 | + PyResult(Ok<T> result_) : result(std::move(result_.value)) {} |
| 246 | + template<typename U> |
| 247 | + constexpr PyResult(Ok<U> result_) : result(static_cast<T>(std::move(result_.value))) |
246 | 248 | { |
247 | 249 | static_assert(std::is_convertible_v<U, T>); |
248 | 250 | } |
249 | | - constexpr PyResult(Err result_) : result(result_) {} |
| 251 | + constexpr PyResult(Err result_) : result(std::unexpected(result_.exc)) {} |
250 | 252 |
|
251 | | - template<typename U> constexpr PyResult(const PyResult<U> &other) : result(Err(nullptr)) |
| 253 | + template<typename U> |
| 254 | + constexpr PyResult(const PyResult<U> &other) |
| 255 | + : result(std::unexpected(static_cast<BaseException *>(nullptr))) |
252 | 256 | { |
253 | 257 | static_assert(std::is_convertible_v<U, T>); |
254 | 258 | if (other.is_ok()) { |
255 | | - result = Ok<T>(other.unwrap()); |
| 259 | + result = static_cast<T>(other.unwrap()); |
256 | 260 | } else { |
257 | | - result = Err(other.unwrap_err()); |
| 261 | + result = std::unexpected(other.unwrap_err()); |
258 | 262 | } |
259 | 263 | } |
260 | 264 |
|
261 | | - bool is_ok() const { return std::holds_alternative<Ok<T>>(result); } |
262 | | - bool is_err() const { return !is_ok(); } |
| 265 | + [[nodiscard]] bool is_ok() const { return result.has_value(); } |
| 266 | + [[nodiscard]] bool is_err() const { return !result.has_value(); } |
263 | 267 |
|
264 | | - const T &unwrap() const & |
| 268 | + [[nodiscard]] const T &unwrap() const & |
265 | 269 | { |
266 | 270 | ASSERT(is_ok()); |
267 | | - return std::get<Ok<T>>(result).value; |
| 271 | + return result.value(); |
268 | 272 | } |
269 | 273 |
|
270 | | - T &unwrap() & |
| 274 | + [[nodiscard]] T &unwrap() & |
271 | 275 | { |
272 | 276 | ASSERT(is_ok()); |
273 | | - return std::get<Ok<T>>(result).value; |
| 277 | + return result.value(); |
274 | 278 | } |
275 | 279 |
|
276 | | - T &&unwrap() && |
| 280 | + [[nodiscard]] T &&unwrap() && |
277 | 281 | { |
278 | 282 | ASSERT(is_ok()); |
279 | | - return std::move(std::get<Ok<T>>(result).value); |
| 283 | + return std::move(result).value(); |
280 | 284 | } |
281 | 285 |
|
282 | | - BaseException *unwrap_err() const |
| 286 | + [[nodiscard]] BaseException *unwrap_err() const |
283 | 287 | { |
284 | 288 | ASSERT(is_err()); |
285 | | - return std::get<Err>(result).exc; |
| 289 | + return result.error(); |
286 | 290 | } |
287 | 291 |
|
288 | 292 | template<typename FunctorType, |
289 | 293 | typename PyResultType = |
290 | 294 | std::conditional_t<detail::is_ok<typename std::invoke_result_t<FunctorType, T>>{}, |
291 | 295 | typename detail::is_ok<typename std::invoke_result_t<FunctorType, T>>::type, |
292 | 296 | typename detail::is_pyresult<typename std::invoke_result_t<FunctorType, T>>::type>> |
293 | | - PyResult<PyResultType> and_then(FunctorType &&op) const; |
| 297 | + [[nodiscard]] PyResult<PyResultType> and_then(FunctorType &&op) const; |
294 | 298 |
|
295 | | - template<typename FunctorType> PyResult<T> or_else(FunctorType &&op) const; |
| 299 | + template<typename FunctorType> [[nodiscard]] PyResult<T> or_else(FunctorType &&op) const; |
296 | 300 | }; |
297 | 301 |
|
298 | 302 | template<typename T> |
|
0 commit comments