-
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
Let the ? operator work natively in try_stream!. #53
Conversation
Insteads of desugaring `?` in the macro, we can have the async block itself return `Result<(), E>`, and adjust the supporting code so that `?` just works. The benefit is that this allows `?` operators that are hidden behind macros.
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.
LGTM, thanks!
Is this supposed to break code that compiled before? Because it does for me. Before this PR one could use "return;" in the stream body to stop yielding. Now this fails with
|
Ah yes, now that I read the PR with some coffee, of course it's supposed to break code. Well, simply returning Ok(()) is not a big deal, and the ability to return an Err is more flexible too. But I would really want a v0.3.1 release of async_stream, which lifts the Unpin on Stream::Item requirement, but with this PR included it would probably have to wait for a v0.4. |
Ah, that's a good point. I didn't realize it, but yes this is a breaking change. I'll leave it to @taiki-e (or other maintainer 🙂) to decide whether to cut a 0.4 or revert this PR. |
@albel727 good catch! I tend to accept this at 0.4, but for now, I'd like to revert this. (and I'd like to add more tests before re-land in 0.4.) |
What is the difference between If the answer is yes, then this leads me to ask: are both fn f<S>(stream: S) -> impl Stream<Item = io::Result<u8>>
where S: Stream<Item = io::Result<&'static str>>
{
try_stream! {
for await s in stream {
let num = s?.parse();
let num = num.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
yield num;
}
}
}
fn g<S>(stream: S) -> impl Stream<Item = io::Result<u8>>
where S: Stream<Item = io::Result<&'static str>>
{
stream! {
for await s in stream {
let num = s?.parse();
let num = num.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
yield Ok(num);
}
}
} |
? cannot be used inside If |
I don't understand. My example code above uses |
Ah, right - it works because The native macro_rules! questionmark {
($e:expr) => { $e? };
}
let _ = stream! {
questionmark!(Err(5));
yield Ok::<i32, i32>(6);
}; Although this case is so rare there's no point in worrying about it much. |
Insteads of desugaring
?
in the macro, we can have the async blockitself return
Result<(), E>
, and adjust the supporting code so that?
just works. The benefit is that this allows?
operators that arehidden behind macros.