Skip to content

Commit

Permalink
Replace manual casts with forward<>() and move()
Browse files Browse the repository at this point in the history
Replace C-style and `static_cast` casts with the appropriate
`std:forward<>()` or `std::move()`.
  • Loading branch information
ispeters committed Aug 29, 2024
1 parent 6ab1716 commit eeb721e
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions include/unifex/connect_awaitable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,26 @@ class _sender_task<Receiver>::type {
bool await_ready() noexcept { return false; }
void await_suspend(coro::coroutine_handle<promise_type>) noexcept(
std::is_nothrow_invocable_v<Func>) {
((Func&&)func_)();
std::forward<Func>(func_)();
}
[[noreturn]] void await_resume() noexcept { std::terminate(); }
};

template <typename Func>
auto yield_value(Func&& func) noexcept {
return awaiter<Func&&>{static_cast<Func&&>(func)};
return awaiter<Func&&>{std::forward<Func>(func)};
}

template <typename Value>
auto await_transform(Value&& value) -> decltype(auto) {
return unifex::await_transform(*this, (Value&&)value);
return unifex::await_transform(*this, std::forward<Value>(value));
}

#if UNIFEX_ENABLE_CONTINUATION_VISITATIONS
template <typename Func>
friend void
tag_invoke(tag_t<visit_continuations>, const promise_type& p, Func&& func) {
visit_continuations(p.receiver_, (Func&&)func);
visit_continuations(p.receiver_, std::forward<Func>(func));
}
#endif

Expand Down Expand Up @@ -133,7 +133,7 @@ inline const struct _fn {
struct _comma_hack {
template <typename T>
friend T&& operator,(T&& t, _comma_hack) noexcept {
return (T&&)t;
return std::forward<T>(t);
}
operator unit() const noexcept { return {}; }
};
Expand Down Expand Up @@ -170,14 +170,14 @@ inline const struct _fn {
unifex::set_value(std::move(receiver));
} else {
unifex::set_value(
std::move(receiver), static_cast<result_type&&>(result));
std::move(receiver), std::forward<result_type>(result));
}
};
// The _comma_hack here makes this well-formed when the co_await
// expression has type void. This could potentially run into trouble
// if the type of the co_await expression itself overloads operator
// comma, but that's pretty unlikely.
}((co_await (Awaitable&&) awaitable, _comma_hack{}));
}((co_await std::move(awaitable), _comma_hack{}));
#if !UNIFEX_NO_EXCEPTIONS
} catch (...) {
ex = std::current_exception();
Expand All @@ -192,7 +192,8 @@ inline const struct _fn {
template <typename Awaitable, typename Receiver>
auto operator()(Awaitable&& awaitable, Receiver&& receiver) const
-> _await::sender_task<remove_cvref_t<Receiver>> {
return connect_impl((Awaitable&&)awaitable, (Receiver&&)receiver);
return connect_impl(
std::forward<Awaitable>(awaitable), std::forward<Receiver>(receiver));
}
} connect_awaitable{};
} // namespace _await_cpo
Expand All @@ -217,13 +218,14 @@ struct _sndr {
static constexpr bool sends_done = true;

explicit type(Awaitable awaitable, instruction_ptr returnAddress)
: awaitable_((Awaitable&&)awaitable)
: awaitable_(std::move(awaitable))
, returnAddress_(returnAddress) {}

template(typename Receiver) //
(requires receiver_of<Receiver, Result>) //
friend auto tag_invoke(tag_t<unifex::connect>, type&& t, Receiver&& r) {
return unifex::connect_awaitable(((type&&)t).awaitable_, (Receiver&&)r);
return unifex::connect_awaitable(
std::move(t).awaitable_, std::forward<Receiver>(r));
}

// TODO: how do we make this property statically discoverable?
Expand Down Expand Up @@ -260,13 +262,14 @@ struct _sndr<Awaitable, void> {

explicit type(Awaitable awaitable, instruction_ptr returnAddress) noexcept(
std::is_nothrow_move_constructible_v<Awaitable>)
: awaitable_((Awaitable&&)awaitable)
: awaitable_(std::move(awaitable))
, returnAddress_(returnAddress) {}

template(typename Receiver) //
(requires receiver_of<Receiver>) //
friend auto tag_invoke(tag_t<unifex::connect>, type&& t, Receiver&& r) {
return unifex::connect_awaitable(((type&&)t).awaitable_, (Receiver&&)r);
return unifex::connect_awaitable(
std::move(t).awaitable_, std::forward<Receiver>(r));
}

// TODO: how do we make this property statically discoverable?
Expand Down Expand Up @@ -295,7 +298,8 @@ struct _fn {
_sender<remove_cvref_t<Awaitable>>
operator()(Awaitable&& awaitable) const {
return _sender<remove_cvref_t<Awaitable>>{
(Awaitable&&)awaitable, instruction_ptr::read_return_address()};
std::forward<Awaitable>(awaitable),
instruction_ptr::read_return_address()};
}
};
} // namespace _as_sender
Expand Down

0 comments on commit eeb721e

Please sign in to comment.