-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module concurrency.operations.dofinally; | ||
|
||
import concurrency; | ||
import concurrency.receiver; | ||
import concurrency.sender; | ||
import concurrency.stoptoken; | ||
import concepts; | ||
|
||
/// runs a side-effect whenever the underlying sender completes | ||
auto doFinally(Sender, SideEffect)(Sender sender, SideEffect effect) { | ||
import concurrency.utils : isThreadSafeFunction; | ||
static assert(isThreadSafeFunction!SideEffect); | ||
return DoFinallySender!(Sender, SideEffect)(sender, effect); | ||
} | ||
|
||
private struct DoFinallyReceiver(Value, SideEffect, Receiver) { | ||
Receiver receiver; | ||
SideEffect sideEffect; | ||
static if (is(Value == void)) | ||
// TODO: mustn't this be nothrow? | ||
void setValue() @safe { | ||
sideEffect(); | ||
receiver.setValue(); | ||
} | ||
|
||
else | ||
void setValue(Value value) @safe { | ||
sideEffect(); | ||
receiver.setValue(value); | ||
} | ||
|
||
void setDone() @safe nothrow { | ||
sideEffect(); | ||
receiver.setDone(); | ||
} | ||
|
||
void setError(Throwable t) @safe nothrow { | ||
sideEffect(); | ||
receiver.setError(t); | ||
} | ||
|
||
mixin ForwardExtensionPoints!receiver; | ||
} | ||
|
||
struct DoFinallySender(Sender, SideEffect) if (models!(Sender, isSender)) { | ||
static assert(models!(typeof(this), isSender)); | ||
alias Value = Sender.Value; | ||
Sender sender; | ||
SideEffect effect; | ||
auto connect(Receiver)(return Receiver receiver) @safe return scope { | ||
// ensure NRVO | ||
auto op = sender.connect( | ||
DoFinallyReceiver!(Sender.Value, SideEffect, Receiver)(receiver, | ||
effect)); | ||
return op; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters