Unverified Commit 2e0c42e8 authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #680

Fix implicit conversion to result<unit_t>
parents 3e98c0ee 6bbc6101
...@@ -126,12 +126,11 @@ public: ...@@ -126,12 +126,11 @@ public:
} }
result(expected<void> x) { result(expected<void> x) {
if (x) { init(x);
flag = rt_value; }
} else {
flag = rt_error; result(expected<unit_t> x) {
err = std::move(x.error()); init(x);
}
} }
result(skip_t) : flag(rt_skip) { result(skip_t) : flag(rt_skip) {
...@@ -142,10 +141,18 @@ public: ...@@ -142,10 +141,18 @@ public:
// nop // nop
} }
result(delegated<unit_t>) : flag(rt_delegated) {
// nop
}
result(const typed_response_promise<void>&) : flag(rt_delegated) { result(const typed_response_promise<void>&) : flag(rt_delegated) {
// nop // nop
} }
result(const typed_response_promise<unit_t>&) : flag(rt_delegated) {
// nop
}
result(const response_promise&) : flag(rt_delegated) { result(const response_promise&) : flag(rt_delegated) {
// nop // nop
} }
...@@ -153,6 +160,17 @@ public: ...@@ -153,6 +160,17 @@ public:
result_runtime_type flag; result_runtime_type flag;
message value; message value;
error err; error err;
private:
template <class T>
void init(T& x) {
if (x) {
flag = rt_value;
} else {
flag = rt_error;
err = std::move(x.error());
}
}
}; };
template <> template <>
......
...@@ -27,6 +27,23 @@ ...@@ -27,6 +27,23 @@
using namespace std; using namespace std;
using namespace caf; using namespace caf;
namespace {
template<class T>
void test_unit_void() {
auto x = result<T>{};
CAF_CHECK_EQUAL(x.flag, rt_value);
x = skip();
CAF_CHECK_EQUAL(x.flag, rt_skip);
x = expected<T>{};
CAF_CHECK_EQUAL(x.flag, rt_value);
x = expected<T>{sec::unexpected_message};
CAF_CHECK_EQUAL(x.flag, rt_error);
CAF_CHECK_EQUAL(x.err, make_error(sec::unexpected_message));
}
} // namespace anonymous
CAF_TEST(skip) { CAF_TEST(skip) {
auto x = result<>{skip()}; auto x = result<>{skip()};
CAF_CHECK_EQUAL(x.flag, rt_skip); CAF_CHECK_EQUAL(x.flag, rt_skip);
...@@ -50,13 +67,9 @@ CAF_TEST(expected) { ...@@ -50,13 +67,9 @@ CAF_TEST(expected) {
} }
CAF_TEST(void_specialization) { CAF_TEST(void_specialization) {
auto x = result<void>{}; test_unit_void<void>();
CAF_CHECK_EQUAL(x.flag, rt_value); }
x = skip();
CAF_CHECK_EQUAL(x.flag, rt_skip); CAF_TEST(unit_specialization) {
x = expected<void>{}; test_unit_void<unit_t>();
CAF_CHECK_EQUAL(x.flag, rt_value);
x = expected<void>{sec::unexpected_message};
CAF_CHECK_EQUAL(x.flag, rt_error);
CAF_CHECK_EQUAL(x.err, make_error(sec::unexpected_message));
} }
...@@ -28,17 +28,23 @@ ...@@ -28,17 +28,23 @@
using namespace caf; using namespace caf;
using unit_res_atom = atom_constant<atom("unitRes")>; using unit_res_atom = atom_constant<atom("unitRes")>;
using void_res_atom = atom_constant<atom("voidRes")>; using void_res_atom = atom_constant<atom("voidRes")>;
using unit_raw_atom = atom_constant<atom("unitRaw")>; using unit_raw_atom = atom_constant<atom("unitRaw")>;
using void_raw_atom = atom_constant<atom("voidRaw")>; using void_raw_atom = atom_constant<atom("voidRaw")>;
using typed_unit_atom = atom_constant<atom("typedUnit")>;
behavior testee(event_based_actor*) { behavior testee(event_based_actor* self) {
return { return {
[] (unit_res_atom) -> result<unit_t> { return unit; }, [] (unit_res_atom) -> result<unit_t> { return unit; },
[] (void_res_atom) -> result<void> { return {}; }, [] (void_res_atom) -> result<void> { return {}; },
[] (unit_raw_atom) -> unit_t { return unit; }, [] (unit_raw_atom) -> unit_t { return unit; },
[] (void_raw_atom) -> void { } [] (void_raw_atom) -> void { },
[=](typed_unit_atom) -> result<unit_t> {
auto rp = self->make_response_promise<unit_t>();
rp.deliver(unit);
return rp;
}
}; };
} }
...@@ -48,7 +54,8 @@ CAF_TEST(unit_results) { ...@@ -48,7 +54,8 @@ CAF_TEST(unit_results) {
scoped_actor self{sys}; scoped_actor self{sys};
auto aut = sys.spawn(testee); auto aut = sys.spawn(testee);
atom_value as[] = {unit_res_atom::value, void_res_atom::value, atom_value as[] = {unit_res_atom::value, void_res_atom::value,
unit_raw_atom::value, void_raw_atom::value}; unit_raw_atom::value, void_raw_atom::value,
typed_unit_atom::value};
for (auto a : as) { for (auto a : as) {
self->request(aut, infinite, a).receive( self->request(aut, infinite, a).receive(
[&] { [&] {
...@@ -60,4 +67,3 @@ CAF_TEST(unit_results) { ...@@ -60,4 +67,3 @@ CAF_TEST(unit_results) {
); );
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment