Skip to content

Commit 4c221e9

Browse files
authored
Merge pull request #268 from elbeno/just-with
💥 Genericize `just`
2 parents 812ccc8 + 53b1461 commit 4c221e9

5 files changed

Lines changed: 70 additions & 21 deletions

File tree

docs/sender_factories.adoc

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ auto sndr = async::just(42, 17);
1313
// sndr produces (sends) the values 42 and 17
1414
----
1515

16-
`just` can be given a name that is used for debug events. By default, its name
17-
is `"just"`.
16+
By default, `just` sends on the value channel, but it can send on any channel.
17+
This is useful for generic code. It can also be given a name that is used for
18+
debug events. By default, its name is either `"just"`, `"just_error"` or
19+
`"just_stopped"` according to the channel selected.
1820

1921
[source,cpp]
2022
----
21-
auto sndr = async::just<"the answer">(42);
23+
auto sndr = async::just<set_value_t, "the answer">(42);
2224
----
2325

2426
[NOTE]
@@ -32,8 +34,7 @@ continuation monad.
3234

3335
Found in the header: `async/just.hpp`
3436

35-
`just_error` is like `just`, but instead of completing by calling `set_value` on
36-
a receiver, it will call `set_error`.
37+
`just_error` is `just<set_error_t>`.
3738

3839
[source,cpp]
3940
----
@@ -129,8 +130,7 @@ auto s = async::just() | async::then([] { return 42; });
129130

130131
Found in the header: `async/just.hpp`
131132

132-
`just_stopped` is like `just`, but instead of completing by calling `set_value` on
133-
a receiver, it will call `set_stopped`.
133+
`just_stopped` is `just<set_stopped_t>`.
134134

135135
[source,cpp]
136136
----
@@ -146,6 +146,26 @@ is `"just_stopped"`.
146146
auto sndr = async::just_stopped<"cancel">();
147147
----
148148

149+
=== `just_value`
150+
151+
Found in the header: `async/just.hpp`
152+
153+
`just_value` is `just<set_value_t>`.
154+
155+
[source,cpp]
156+
----
157+
auto sndr = async::just_value(42);
158+
// sndr produces (sends) the value 42 on the value channel
159+
----
160+
161+
`just_value` can be given a name that is used for debug events. By default, its name
162+
is `"just_value"`.
163+
164+
[source,cpp]
165+
----
166+
auto sndr = async::just_value<"oops">(42);
167+
----
168+
149169
=== `read_env`
150170

151171
Found in the header: `async/read_env.hpp`

docs/synopsis.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@
6565
* `into_variant` - a xref:sender_adaptors.adoc#_into_variant[sender adaptor] that collapses value completions into a variant
6666

6767
==== https://github.com/intel/cpp-baremetal-senders-and-receivers/blob/main/include/async/just.hpp[just.hpp]
68-
* `just` - a xref:sender_factories.adoc#_just[sender factory] that sends on the value channel
68+
* `just` - a xref:sender_factories.adoc#_just[sender factory] that sends on the selected channel (value by default)
6969
* `just_error` - a xref:sender_factories.adoc#_just_error[sender factory] that sends on the error channel
7070
* `just_stopped` - a xref:sender_factories.adoc#_just_stopped[sender factory] that sends on the stopped channel
71+
* `just_value` - a xref:sender_factories.adoc#_just[sender factory] that sends on the value channel
7172

7273
==== https://github.com/intel/cpp-baremetal-senders-and-receivers/blob/main/include/async/just_result_of.hpp[just_result_of.hpp]
7374
* `just_error_result_of` - a xref:sender_factories.adoc#_just_error_result_of[sender factory] that sends lazily computed values on the error channel
@@ -253,6 +254,7 @@ contains traits and metaprogramming constructs used by many senders.
253254
* xref:sender_factories.adoc#_just_error_result_of[`just_error_result_of`] - https://github.com/intel/cpp-baremetal-senders-and-receivers/blob/main/include/async/just_result_of.hpp[`#include <async/just_result_of.hpp>`]
254255
* xref:sender_factories.adoc#_just_result_of[`just_result_of`] - https://github.com/intel/cpp-baremetal-senders-and-receivers/blob/main/include/async/just_result_of.hpp[`#include <async/just_result_of.hpp>`]
255256
* xref:sender_factories.adoc#_just_stopped[`just_stopped`] - https://github.com/intel/cpp-baremetal-senders-and-receivers/blob/main/include/async/just.hpp[`#include <async/just.hpp>`]
257+
* xref:sender_factories.adoc#_just_value[`just_value`] - https://github.com/intel/cpp-baremetal-senders-and-receivers/blob/main/include/async/just.hpp[`#include <async/just.hpp>`]
256258
* xref:sender_adaptors.adoc#_let_error[`let_error`] - https://github.com/intel/cpp-baremetal-senders-and-receivers/blob/main/include/async/let_error.hpp[`#include <async/let_error.hpp>`]
257259
* xref:sender_adaptors.adoc#_let_stopped[`let_stopped`] - https://github.com/intel/cpp-baremetal-senders-and-receivers/blob/main/include/async/let_stopped.hpp[`#include <async/let_stopped.hpp>`]
258260
* xref:sender_adaptors.adoc#_let_value[`let_value`] - https://github.com/intel/cpp-baremetal-senders-and-receivers/blob/main/include/async/let_value.hpp[`#include <async/let_value.hpp>`]

include/async/just.hpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,49 @@ template <stdx::ct_string Name, typename Tag, typename... Vs> struct sender {
6363
return prop{completes_synchronously_t{}, std::true_type{}};
6464
}
6565
};
66+
67+
template <channel_tag Tag> constexpr inline auto name = stdx::ct_string{""};
68+
69+
template <>
70+
constexpr inline auto name<set_value_t> = stdx::ct_string{"just_value"};
71+
template <>
72+
constexpr inline auto name<set_error_t> = stdx::ct_string{"just_error"};
73+
template <>
74+
constexpr inline auto name<set_stopped_t> = stdx::ct_string{"just_stopped"};
6675
} // namespace _just
6776

68-
template <stdx::ct_string Name = "just", typename... Vs>
77+
template <channel_tag Tag = set_value_t,
78+
stdx::ct_string Name = _just::name<Tag>, typename... Vs>
6979
[[nodiscard]] constexpr auto just(Vs &&...vs) -> sender auto {
70-
return _just::sender<Name, set_value_t, std::remove_cvref_t<Vs>...>{
80+
static_assert(sizeof...(Vs) == 0 or not std::same_as<Tag, set_stopped_t>,
81+
"Can't send any values on the stopped channel");
82+
83+
return _just::sender<Name, Tag, std::remove_cvref_t<Vs>...>{
7184
{std::forward<Vs>(vs)...}};
7285
}
7386

74-
template <stdx::ct_string Name = "just_error", typename... Vs>
87+
template <stdx::ct_string Name = _just::name<set_value_t>, typename... Vs>
88+
[[nodiscard]] constexpr auto just_value(Vs &&...vs) -> sender auto {
89+
return just<set_value_t, Name>(std::forward<Vs>(vs)...);
90+
}
91+
92+
template <stdx::ct_string Name = _just::name<set_error_t>, typename... Vs>
7593
[[nodiscard]] constexpr auto just_error(Vs &&...vs) -> sender auto {
76-
return _just::sender<Name, set_error_t, std::remove_cvref_t<Vs>...>{
77-
{std::forward<Vs>(vs)...}};
94+
return just<set_error_t, Name>(std::forward<Vs>(vs)...);
7895
}
7996

80-
template <stdx::ct_string Name = "just_stopped">
97+
template <stdx::ct_string Name = _just::name<set_stopped_t>>
8198
[[nodiscard]] constexpr auto just_stopped() -> sender auto {
82-
return _just::sender<Name, set_stopped_t>{};
99+
return just<set_stopped_t, Name>();
83100
}
84101

85-
struct just_t;
102+
struct just_value_t;
86103
struct just_error_t;
87104
struct just_stopped_t;
88105

89106
template <typename Tag>
90107
using just_tag_for =
91-
stdx::conditional_t<std::same_as<Tag, set_value_t>, just_t,
108+
stdx::conditional_t<std::same_as<Tag, set_value_t>, just_value_t,
92109
stdx::conditional_t<std::same_as<Tag, set_error_t>,
93110
just_error_t, just_stopped_t>>;
94111

include/async/variant_sender.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ constexpr auto make_variant_sender(bool b, F1 &&f1, F2 &&f2) {
277277

278278
template <stdx::callable F> constexpr auto make_optional_sender(bool b, F &&f) {
279279
return make_variant_sender(b, std::forward<F>(f),
280-
[] { return just<"opt-else">(); });
280+
[] { return just<set_value_t, "opt-else">(); });
281281
}
282282

283283
struct variant_t;

test/just.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ TEST_CASE("just op state is synchronous", "[just]") {
8989

9090
template <>
9191
inline auto async::injected_debug_handler<> =
92-
debug_handler<async::just_t, true>{};
92+
debug_handler<async::just_value_t, true>{};
9393

9494
TEST_CASE("just can be debugged with a string", "[just]") {
9595
using namespace std::string_literals;
@@ -100,13 +100,14 @@ TEST_CASE("just can be debugged with a string", "[just]") {
100100
async::prop{async::get_debug_interface_t{},
101101
async::debug::named_interface<"op">{}}});
102102
async::start(op);
103-
CHECK(debug_events == std::vector{"op just start"s, "op just set_value"s});
103+
CHECK(debug_events ==
104+
std::vector{"op just_value start"s, "op just_value set_value"s});
104105
}
105106

106107
TEST_CASE("just can be named and debugged with a string", "[just]") {
107108
using namespace std::string_literals;
108109
debug_events.clear();
109-
auto s = async::just<"just_name">();
110+
auto s = async::just_value<"just_name">();
110111
auto op = async::connect(
111112
s, with_env{universal_receiver{},
112113
async::prop{async::get_debug_interface_t{},
@@ -115,3 +116,12 @@ TEST_CASE("just can be named and debugged with a string", "[just]") {
115116
CHECK(debug_events ==
116117
std::vector{"op just_name start"s, "op just_name set_value"s});
117118
}
119+
120+
TEST_CASE("just<> is equivalent to just_value", "[just]") {
121+
int value{};
122+
auto s = async::just_value(42);
123+
auto r = receiver{[&](auto i) { value = i; }};
124+
auto op = async::connect(s, r);
125+
async::start(op);
126+
CHECK(value == 42);
127+
}

0 commit comments

Comments
 (0)