Add extension methods for using a StopToken, and use Result instead of Option #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I found myself writing the following code often:
Adding an extension method makes this more readable, since a fluent style can be used:
In addition, I often had to use
match
to check for theNone
value being returned. This make the code very verbose.Using
Result
instead makes it possible to use the question mark style for early return upon a droppedStopSource
.Unfortunately this can be done only for
StopFuture
and not forStopStream
, since streams useOption
explicitly.But it still remains worthwhile to use it for futures.
Oh, and one more thing: Using a
Result
makes it possible to add more types of early termination, such as via a timeout instead of via the stop token. I often find myself setting up a timeout explicitly, and it would be nice to be able to add a timeout as well via a method -- and then receive a differentError
value to be able to match on that.My codebase (unfortunately not Open Source at this moment) became much cleaner with these changes.
The test I added demonstrates the usage.
@matklad @dignifiedquire I'd love your feedback on this.