Skip to content

Commit

Permalink
Stop requiring rvalue references for co_return
Browse files Browse the repository at this point in the history
  • Loading branch information
dermesser committed Aug 14, 2024
1 parent 066c72a commit e942458
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 22 deletions.
20 changes: 10 additions & 10 deletions test/async_work_test.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@

#include <atomic>
#include <functional>
#include <stdexcept>
#include <thread>
#include <fcntl.h>
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <gtest/gtest.h>
#include <uv.h>

#include "test_util.h"

#include "uvco/async_work.h"
#include "uvco/loop/loop.h"
#include "uvco/promise/promise.h"

#include <fcntl.h>
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <gtest/gtest.h>
#include <atomic>
#include <functional>
#include <stdexcept>
#include <thread>
#include <vector>

namespace {
Expand Down Expand Up @@ -115,7 +114,8 @@ TEST(AsyncWorkTest, execptionThrownForValue) {
}

TEST(AsyncWorkTest, workNotAwaited) {
// Test that work on the threadpool finishes even if the main function returns early.
// Test that work on the threadpool finishes even if the main function returns
// early.
bool workRan = false;
auto work = [&workRan]() -> void {
std::this_thread::sleep_for(std::chrono::milliseconds{10});
Expand Down
2 changes: 1 addition & 1 deletion test/pqxx_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ constexpr std::string_view schemaTearDown = R"(
DROP TABLE test;
)";

constexpr std::string_view connectionString = "dbname=lbo";
constexpr std::string_view connectionString = "dbname=lbo user=lbo";

// This test obviously only runs if you have a PostgreSQL server running. Change
// the connection string to match your setup.
Expand Down
2 changes: 1 addition & 1 deletion uvco/async_work.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Promise<R> submitWork(const Loop &loop, std::function<R()> work) {
if (result->index() == 1) {
std::rethrow_exception(std::get<std::exception_ptr>(*result));
}
co_return std::move(std::get<R>(*result));
co_return std::get<R>(std::move(*result));
}

template <>
Expand Down
9 changes: 1 addition & 8 deletions uvco/promise/multipromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,20 +309,13 @@ template <typename T> class Generator {
/// still moved into the generator's slot, but the generator is not resumed.
/// Upon the next `co_await`, the returned `MultiPromiseAwaiter_` will
/// immediately return the value without resuming the generator.
YieldAwaiter_ yield_value(T &&value) {
YieldAwaiter_ yield_value(T value) {
BOOST_ASSERT(!core_->slot);
core_->slot = std::move(value);
core_->resume();
return YieldAwaiter_{*core_};
}

YieldAwaiter_ yield_value(const T &value) {
BOOST_ASSERT(!core_->slot);
core_->slot = value;
core_->resume();
return YieldAwaiter_{*core_};
}

private:
/// A `YieldAwaiter_` suspends a coroutine returning a MultiPromise (i.e. a
/// generator) and resumes it when the value is read by the awaiting
Expand Down
2 changes: 1 addition & 1 deletion uvco/promise/promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ template <typename T> class Coroutine {

/// Part of the coroutine protocol: Called by `co_return`. Schedules the
/// awaiting coroutine for resumption.
void return_value(T &&value) {
void return_value(T value) {
// Probably cancelled.
if (core_->slot.has_value() && core_->slot->index() == 1) {
return;
Expand Down
2 changes: 1 addition & 1 deletion uvco/uds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Promise<UnixStream> UnixStreamClient::connect(std::string_view path) {
// open but not connected handle.
try {
UnixStream connection = co_await awaiter;
co_return std::move(connection);
co_return connection;
} catch (const UvcoException &e) {
maybeError = e;
}
Expand Down

0 comments on commit e942458

Please sign in to comment.