Skip to content

Commit f512e78

Browse files
committed
fix Result<T>
1 parent b25213c commit f512e78

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/cmake-build-*
33
.idea
44
*.out
5-
tt.py
5+
/*.py

include/asyncio/result.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ASYNCIO_NS_BEGIN
1313
template<typename T>
1414
struct Result {
1515
constexpr bool has_value() const noexcept {
16-
return std::get_if<T>(&result_) != nullptr;
16+
return std::get_if<std::monostate>(&result_) == nullptr;
1717
}
1818
template<typename R>
1919
constexpr void set_value(R&& value) noexcept {
@@ -53,19 +53,21 @@ struct Result {
5353

5454
template<>
5555
struct Result<void> {
56-
bool has_value() const noexcept {
57-
return result_ != nullptr;
56+
constexpr bool has_value() const noexcept {
57+
return result_.has_value();
58+
}
59+
constexpr void return_void() noexcept {
60+
result_.emplace(nullptr);
5861
}
59-
constexpr void return_void() noexcept { }
6062
void result() {
61-
if (result_) { std::rethrow_exception(result_); }
63+
if (result_.has_value() && *result_ != nullptr) { std::rethrow_exception(*result_); }
6264
}
6365

6466
void set_exception(std::exception_ptr exception) noexcept { result_ = exception; }
6567
void unhandled_exception() noexcept { result_ = std::current_exception(); }
6668

6769
private:
68-
std::exception_ptr result_;
70+
std::optional<std::exception_ptr> result_;
6971
};
7072

7173
ASYNCIO_NS_END

include/asyncio/wait_for.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ASYNCIO_NS_BEGIN
1515
namespace detail {
1616
template<typename R, typename Duration>
1717
struct WaitForAwaiter: NonCopyable {
18-
constexpr bool await_ready() noexcept { return false; }
18+
constexpr bool await_ready() noexcept { return result_.has_value(); }
1919
constexpr decltype(auto) await_resume() {
2020
return result_.result();
2121
}

0 commit comments

Comments
 (0)