Skip to content

Commit

Permalink
Allow to then on functions too
Browse files Browse the repository at this point in the history
  • Loading branch information
skoppe committed Jul 20, 2021
1 parent 0558f81 commit 1d339d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions source/concurrency/operations/then.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import concurrency.receiver;
import concurrency.sender;
import concurrency.stoptoken;
import concepts;
import std.traits;

auto then(Sender, Fun)(Sender sender, Fun fun) {
static assert (hasFunctionAttributes!(Fun, "shared"), "Function must be shared");
import std.traits : hasFunctionAttributes, isFunction, isFunctionPointer;
static assert (isFunction!Fun || isFunctionPointer!Fun || hasFunctionAttributes!(Fun, "shared"), "Function must be shared");

return ThenSender!(Sender, Fun)(sender, fun);
}

private struct ThenReceiver(Receiver, Value, Fun) {
import std.traits : ReturnType;
Receiver receiver;
Fun fun;
static if (is(Value == void)) {
Expand Down
7 changes: 6 additions & 1 deletion tests/ut/concurrency/operations.d
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,16 @@ unittest {
VoidSender().via(ValueSender!int(4)).syncWait.value.should == 4;
}

@("then.value")
@("then.value.delegate")
@safe unittest {
ValueSender!int(3).then((int i) shared => i*3).syncWait.value.shouldEqual(9);
}

@("then.value.function")
@safe unittest {
ValueSender!int(3).then((int i) => i*3).syncWait.value.shouldEqual(9);
}

@("then.oob")
@safe unittest {
OutOfBandValueSender!int(46).then((int i) shared => i*3).syncWait.value.shouldEqual(138);
Expand Down

0 comments on commit 1d339d8

Please sign in to comment.