Skip to content

Commit 853ddbe

Browse files
committed
runtime: Upgrade to C++23 and use std::expected
1 parent 4e55dbe commit 853ddbe

2 files changed

Lines changed: 25 additions & 21 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)
55

66
project(python++)
77

8-
set(CMAKE_CXX_STANDARD 20)
8+
set(CMAKE_CXX_STANDARD 23)
99

1010
include(cmake/CPM.cmake)
1111

src/runtime/Value.hpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <algorithm>
1111
#include <cmath>
1212
#include <cstddef>
13+
#include <expected>
1314
#include <sstream>
1415
#include <variant>
1516
#include <vector>
@@ -230,69 +231,72 @@ namespace detail {
230231
}// namespace detail
231232

232233

233-
template<typename T> class PyResult
234+
template<typename T> class [[nodiscard]] PyResult
234235
{
235236
public:
236237
using OkType = T;
237238
using ErrType = BaseException *;
238-
using StorageType = std::variant<Ok<T>, Err>;
239+
using StorageType = std::expected<T, BaseException *>;
239240

240241
private:
241242
StorageType result;
242243

243244
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)))
246248
{
247249
static_assert(std::is_convertible_v<U, T>);
248250
}
249-
constexpr PyResult(Err result_) : result(result_) {}
251+
constexpr PyResult(Err result_) : result(std::unexpected(result_.exc)) {}
250252

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)))
252256
{
253257
static_assert(std::is_convertible_v<U, T>);
254258
if (other.is_ok()) {
255-
result = Ok<T>(other.unwrap());
259+
result = static_cast<T>(other.unwrap());
256260
} else {
257-
result = Err(other.unwrap_err());
261+
result = std::unexpected(other.unwrap_err());
258262
}
259263
}
260264

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(); }
263267

264-
const T &unwrap() const &
268+
[[nodiscard]] const T &unwrap() const &
265269
{
266270
ASSERT(is_ok());
267-
return std::get<Ok<T>>(result).value;
271+
return result.value();
268272
}
269273

270-
T &unwrap() &
274+
[[nodiscard]] T &unwrap() &
271275
{
272276
ASSERT(is_ok());
273-
return std::get<Ok<T>>(result).value;
277+
return result.value();
274278
}
275279

276-
T &&unwrap() &&
280+
[[nodiscard]] T &&unwrap() &&
277281
{
278282
ASSERT(is_ok());
279-
return std::move(std::get<Ok<T>>(result).value);
283+
return std::move(result).value();
280284
}
281285

282-
BaseException *unwrap_err() const
286+
[[nodiscard]] BaseException *unwrap_err() const
283287
{
284288
ASSERT(is_err());
285-
return std::get<Err>(result).exc;
289+
return result.error();
286290
}
287291

288292
template<typename FunctorType,
289293
typename PyResultType =
290294
std::conditional_t<detail::is_ok<typename std::invoke_result_t<FunctorType, T>>{},
291295
typename detail::is_ok<typename std::invoke_result_t<FunctorType, T>>::type,
292296
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;
294298

295-
template<typename FunctorType> PyResult<T> or_else(FunctorType &&op) const;
299+
template<typename FunctorType> [[nodiscard]] PyResult<T> or_else(FunctorType &&op) const;
296300
};
297301

298302
template<typename T>

0 commit comments

Comments
 (0)