-
Notifications
You must be signed in to change notification settings - Fork 35
Add for_await to process streams using a for loop #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
027896c
Add for_await to process streams using a for loop
taiki-e 8332d22
Merge branch 'master' into for-await
taiki-e 45dfceb
#[for_await] -> #{await]
taiki-e 83fdd7e
cargo fmt
taiki-e c1d4aef
for await
taiki-e 748d3b8
simplify
taiki-e 89fa93e
Merge branch 'master' into for-await
carllerche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -20,7 +20,7 @@ repository = "https://github.com/tokio-rs/async-stream" | |
readme = "README.md" | ||
|
||
[dependencies] | ||
async-stream-impl = { version = "0.1.0" } | ||
async-stream-impl = { version = "0.1.0", path = "../async-stream-impl" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must be revert before merging. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine to keep the path in here IMO. |
||
futures-core-preview = "=0.3.0-alpha.18" | ||
|
||
[dev-dependencies] | ||
|
This file contains hidden or 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 hidden or 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,32 @@ | ||
use futures_core::Stream; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
// This is equivalent to the `futures::StreamExt::next` method. | ||
// But we want to make this crate dependency as small as possible, so we define our `next` function. | ||
#[doc(hidden)] | ||
pub fn next<S>(stream: &mut S) -> impl Future<Output = Option<S::Item>> + '_ | ||
where | ||
S: Stream + Unpin, | ||
{ | ||
Next { stream } | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Next<'a, S> { | ||
stream: &'a mut S, | ||
} | ||
|
||
impl<S> Unpin for Next<'_, S> where S: Unpin {} | ||
|
||
impl<S> Future for Next<'_, S> | ||
where | ||
S: Stream + Unpin, | ||
{ | ||
type Output = Option<S::Item>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Pin::new(&mut self.stream).poll_next(cx) | ||
} | ||
} |
This file contains hidden or 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,25 @@ | ||
#![feature(async_await)] | ||
|
||
use tokio::prelude::*; | ||
|
||
use async_stream::stream; | ||
|
||
#[tokio::test] | ||
async fn test() { | ||
let s = stream! { | ||
yield "hello"; | ||
yield "world"; | ||
}; | ||
|
||
let s = stream! { | ||
for await x in s { | ||
yield x.to_owned() + "!"; | ||
} | ||
}; | ||
|
||
let values: Vec<_> = s.collect().await; | ||
|
||
assert_eq!(2, values.len()); | ||
assert_eq!("hello!", values[0]); | ||
assert_eq!("world!", values[1]); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about just
#[await]
?Also, since this is a macro anyway, we could potentially get more aggressive and transform:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me.
I think syn doesn't support this syntax yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(By the way, that TODO comment is about the current implementation confirming that the number of attributes is exactly one.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syn
doesn't support it... but it may not be too hard to impl. You would have to manually walk theTokenStream
and findfor
followed byawait
, transform that to#[await] for
then run ^^.That can be follow up work though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in c1d4aef