-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add stream
and try_stream
functions
#74
base: master
Are you sure you want to change the base?
Conversation
I think the lack of the |
cc @carllerche @Darksonn @Kestrer: any thoughts on this? |
I would want a better understanding of how it fails if used in these. |
The following code is incorrect: join!(
async { stream.yield_item(1).await; },
async { stream.yield_item(2).await; },
); because it will result in two items being yielded within the same poll, which results in a panic. For the try_join!(
async { stream.yield_item(1).await; },
async { something_that_fails()?; Ok(()) },
)?; Because an item will be yielded but then the Of course, making these operations panic is a choice. Alternatives to the first case include:
For the second case, we could also just obediently yield the item. |
Having this fail seems pretty confusing. I would certainly prefer a version where this works. Perhaps join!(
async { stream.yield_item(1).await; },
async { stream.yield_item(2).await; },
); |
Oh wait, I was mistaken — let stream_2 = stream.clone();
join!(
async { stream.yield_item(1).await; },
async { stream_2.yield_item(2).await; },
); and this code would panic. So, we could remove the Another option is, as you said, to have |
This is so much more user friendly. I would love to see this get merged! I don't know what the next steps should be but maybe you could resolve the conflicts please? |
Would love to see this added. |
I don't know if this would be possible but it seems like it's not
|
You’re right — I have now pushed a fix and tests for that. |
Is there interest at all by the maintainers to integrate this? If so what would the steps be? Maybe check/reduce breakage of existing code and document an upgrade path? (Also merge conflicts again) I looked over the code and it didn't look too bad. There are a few things I'm not familiar with though like manual future implementations. |
Maybe the macro feature should be enabled by default to aid in compatiblity? |
Another upside of this approach: Unlike yield, allows defining of a custom Ext trait. E.g. |
I suggest a shorter name |
Note that this following code is incorrect: Case Itokio::task(async move {
stream.yield_item(1).await;
}); Case IIlet _ = || async move {
stream.yield_item(1).await;
}; Case IIIlet mut _stream = None;
let s = stream(|stream| {
_stream = Some(stream);
async {}
}); And much more... SolutionInstead of stream|mut s| async {
s.yield_(42).await;
return (s, "42");
}); I create an alternative to this crate called |
Example usage:
Advantages of using the new API:
stream::<u32, _, _>
ortry_stream::<u32, io::Error, _, _>
or|stream: async_stream::Stream<u32>|
rustfmt
syn
Disadvatanges of using the new API:
stream.yield_item(i).await
versusyield i;
)stream
variable is moved outside the stream, used insidejoin!
orselect!
orFuturesUnordered
etcOpen questions:
stream
andStream
which I was inspired bythread::scope
for, but there may be a better name.closes #71, closes #56, closes #63, closes #64 (latter two are via #56)
Might close, if new API is considered sufficient: #33, #68