Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Future::on_completion #5181

Merged
merged 2 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/realm/util/future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
**************************************************************************/

#pragma once

#include <condition_variable>
#include <mutex>
#include <type_traits>
Expand Down Expand Up @@ -793,32 +795,32 @@ class REALM_NODISCARD future_details::Future {
using Wrapper = StatusOrStatusWith<T>;
using Result = NormalizedCallResult<Func, StatusOrStatusWith<T>>;
if constexpr (!is_future<Result>) {
return generalImpl(
return general_impl(
// on ready success:
[&](T&& val) {
return Future<Result>::make_ready(
no_throw_call(std::forward<Func>(func), Wrapper(std::move(val))));
},
// on ready failure:
[&](Status&& status) {
return Future<Result>::make_Ready(
return Future<Result>::make_ready(
no_throw_call(std::forward<Func>(func), Wrapper(std::move(status))));
},
// on not ready yet:
[&] {
return make_continuation<Result>(
[func = std::forward<Func>(func)](SharedState<T>* input,
SharedState<Result>* output) mutable noexcept {
if (!input->status.is_ok())
return output->set_from(no_throw_call(func, Wrapper(std::move(input->status))));
if (!input->m_status.is_ok())
return output->set_from(no_throw_call(func, Wrapper(std::move(input->m_status))));

output->set_from(no_throw_call(func, Wrapper(std::move(*input->data))));
output->set_from(no_throw_call(func, Wrapper(std::move(*input->m_data))));
});
});
}
else {
using UnwrappedResult = typename Result::value_type;
return generalImpl(
return general_impl(
// on ready success:
[&](T&& val) {
try {
Expand All @@ -844,23 +846,23 @@ class REALM_NODISCARD future_details::Future {
return make_continuation<UnwrappedResult>(
[func = std::forward<Func>(func)](SharedState<T>* input,
SharedState<UnwrappedResult>* output) mutable noexcept {
if (!input->status.isOK()) {
if (!input->m_status.is_ok()) {
try {
throwing_call(func, Wrapper(std::move(input->m_status)))
.propagate_result_to(output);
}
catch (...) {
output->set_error(exception_to_status());
output->set_status(exception_to_status());
}

return;
}

try {
throwing_call(func, Wrapper(std::move(*input->data))).propagate_result_to(output);
throwing_call(func, Wrapper(std::move(*input->m_data))).propagate_result_to(output);
}
catch (...) {
output->set_error(exception_to_status());
output->set_status(exception_to_status());
}
});
});
Expand Down Expand Up @@ -937,6 +939,8 @@ class REALM_NODISCARD future_details::Future {
}
}

Future<void> ignore_value() && noexcept;

private:
template <typename T2>
friend class Future;
Expand Down Expand Up @@ -1115,7 +1119,13 @@ class REALM_NODISCARD future_details::Future<void> {
return std::move(inner).on_error(std::forward<Func>(func));
}

Future<void> ignoreValue() && noexcept
template <typename Func>
auto on_completion(Func&& func) && noexcept
{
return std::move(inner).on_completion(std::forward<Func>(func));
}

Future<void> ignore_value() && noexcept
{
return std::move(*this);
}
Expand Down Expand Up @@ -1207,4 +1217,10 @@ inline void Promise<T>::set_from(Future<T>&& future) noexcept
});
}

template <typename T>
inline Future<void> Future<T>::ignore_value() && noexcept
{
return std::move(*this).then([](auto&&) {});
}

} // namespace realm::util
Loading