Closed
Description
Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=3d9fb14075d8fb9de8dea454fbb663e9
Code
#![feature(generator_trait)]
use std::ops::{Generator, GeneratorState};
struct GeneratorIterator<T> {
generator: T,
}
impl<T: Generator<Return = ()>> Iterator for GeneratorIterator<T> {
type Item = T::Yield;
fn next(&mut self) -> Option<Self::Item> {
match self.generator.resume() {
GeneratorState::Yielded(x) => Some(x),
GeneratorState::Complete(()) => None,
}
}
}
pub fn gen<Item, T: Generator<Yield = Item, Return = ()>>(
generator: T,
) -> impl Iterator<Item = Item> {
GeneratorIterator { generator }
}
fn main() {
println!("Hello, world!");
}
Error message:
Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `resume` found for type `T` in the current scope
--> src/main.rs:13:30
|
13 | match self.generator.resume() {
| ^^^^^^ method not found in `T`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `resume`, perhaps you need to restrict type parameter `T` with it:
|
9 | impl<T: std::ops::Generator + Generator<Return = ()>> Iterator for GeneratorIterator<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground`.
The error message hint is unhelpful because Generator
is already a bound for T
. Note that Generator::resume
takes a Pin<&mut Self>
as its self
.