Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upUsing a stream after the `for` loop #17
Comments
This comment has been minimized.
This comment has been minimized.
Arnavion
commented
Sep 9, 2017
|
The user can just give a |
This comment has been minimized.
This comment has been minimized.
|
Unfortunately right now due to borrowing restrictions in generators that won't work :( |
This comment has been minimized.
This comment has been minimized.
Arnavion
commented
Sep 10, 2017
|
Ah, you're right. I guess then the current workaround is to manually loop over fn foo<'a, S: Stream<Item = i32, Error = &'static str> + 'a>(mut s: S) -> impl Future<Item = (), Error = &'static str> + 'a {
::async_block! {
loop {
match ::await!(s.into_future()) {
Ok((Some(i), rest)) => {
assert_eq!(i, 5);
s = rest;
// Only consume the first element.
break;
},
Ok((None, rest)) | Err((_, rest)) => {
s = rest;
break;
},
}
}
assert_eq!(s.poll(), Ok(Async::Ready(Some(6))));
Ok(())
}
} |
This comment has been minimized.
This comment has been minimized.
|
Yeah I'd just hope we could get it a little more ergonomic than that :( |
This comment has been minimized.
This comment has been minimized.
|
I believe this is solved by |
withoutboats
closed this
Mar 11, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
alexcrichton commentedSep 3, 2017
Right now
#[async] forwill consume its stream argument (likeforloops and iterators). Unlike iterators, however, there's not a great way to consume only half the stream and then continue using it afterwards (for example to break out early or reclaim resources).With for loops you've got:
but there's not quite the same for streams!