NVIDIA / stdexec

`std::execution`, the proposed C++ framework for asynchronous and parallel programming.
Apache License 2.0
1.52k stars 153 forks source link

add sender factory `exec::just_from` #1386

Closed ericniebler closed 1 month ago

ericniebler commented 1 month ago

just_from(fn) creates a sender that completes inline by passing a "sink" function to fn. Calling the sink function with arguments sends the arguments as values to the receiver.

The sink function passed to fn must be called exactly once.

Example:

// The following sender is equivalent to just(42, 3.14):
auto sndr = exec::just_from([](auto sink) { return sink(42, 3.14); });

The function passed to just_from must return an instance of a specialization of stdexec::completion_signatures<> that describes the ways the sink function might be invoked. The sink function returns such a specialization of stdexec::completion_signatures<> corresponding to the arguments passed to it, but if your function uses the sink function in several different ways, you must specify the return type explicitly.

Example:

auto sndr = exec::just_from(
  [](auto sink) {
    if (some-condition) {
      sink(42);
    } else {
      sink(3.14);
    }
    return stdexec::completion_signatures<stdexec::set_value_t(int),
                                          stdexec::set_value_t(double)>{};
  });