Add stream and try_stream functions - #74
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 |
|
I just found this PR after filed #114. |
Example usage:
Advantages of using the new API:
stream::<u32, _, _>ortry_stream::<u32, io::Error, _, _>or|stream: async_stream::Stream<u32>|rustfmtsynDisadvatanges of using the new API:
stream.yield_item(i).awaitversusyield i;)streamvariable is moved outside the stream, used insidejoin!orselect!orFuturesUnorderedetcOpen questions:
streamandStreamwhich I was inspired bythread::scopefor, 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