-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add core::ops::GeneratorState::{is_yielded,is_complete}
methods
#88154
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 core::ops::GeneratorState::{is_yielded,is_complete}
methods
#88154
Conversation
r? @dtolnay (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
Similar to Option::is_ and Result::is_ methods, this adds is_ shorthands for GeneratorState.
Thanks! Can you give an example of a concrete use case? |
Sure thing! I only recently started looking at generators more closely, so I don't have loads to back it up in terms of experience. But one thing where these methods are particularly useful for is when validating generator state. For example, we could rewrite the main generator example using #![feature(generators, generator_trait)]
use std::ops::{Generator, GeneratorState};
use std::pin::Pin;
fn main() {
let mut generator = || {
yield 1;
return "foo"
};
- match Pin::new(&mut generator).resume(()) {
- GeneratorState::Yielded(1) => {}
- _ => panic!("unexpected return from resume"),
- }
+ assert!(Pin::new(&mut generator).resume(()).is_yielded());
- match Pin::new(&mut generator).resume(()) {
- GeneratorState::Complete("foo") => {}
- _ => panic!("unexpected return from resume"),
- }
+ assert!(Pin::new(&mut generator).resume(()).is_complete());
} I don't know how much manual generator impls, generator state introspection, and |
9950: Fix codegen for is_method documentation r=yoshuawuyts a=yoshuawuyts While authoring rust-lang/rust#88154 I realized that the codegen for the `enum_generate_is_method` assist currently generates invalid paths, and used snake case instead of spaces for the docs description. This fixes both issues. Thanks! Co-authored-by: Yoshua Wuyts <[email protected]>
Sounds like it should also have things like I'm not necessarily opposed to adding |
Hmm, yeah that seems like it would be useful indeed. Thinking about this a bit: if we consider
This could equally be applied to It definitely seems like it would've made sense to keep these types consistent from the start. But because late is better than never, we might as well do it now. If this reasoning all makes sense; what would the right way be to proceed with this? I believe libs has both RFCs and policies; but I'm not sure which one this would fall under? |
That will lose informations about the values that were yielded though. |
@SkiFire13 I'm not proposing we change the example; keeping as much information as possible makes sense here. It merely served to illustrate that when folks author their own generators by hand, being able to assert intermediate states can sometimes be simplified by providing |
An RFC would work well for this kind of thing. |
@yoshuawuyts any updates on the rfc for this change? |
Closing this as its inactive and waiting on a rfc that will take time to merge. |
Similar to
Option::is_*
,Result::is_*
andControlFlow::is_*
methods, this addsis_*
methods forGeneratorState
. Thanks!