2018-12-03 07:13:28 -06:00
|
|
|
#![allow(unused_mut)]
|
|
|
|
#![feature(generators, generator_trait)]
|
|
|
|
|
2018-10-04 13:49:38 -05:00
|
|
|
use std::marker::Unpin;
|
2018-12-03 07:13:28 -06:00
|
|
|
use std::ops::Generator;
|
|
|
|
use std::ops::GeneratorState::Yielded;
|
2018-10-04 13:49:38 -05:00
|
|
|
use std::pin::Pin;
|
2018-12-03 07:13:28 -06:00
|
|
|
|
|
|
|
pub struct GenIter<G>(G);
|
|
|
|
|
|
|
|
impl <G> Iterator for GenIter<G>
|
|
|
|
where
|
2018-10-04 13:49:38 -05:00
|
|
|
G: Generator + Unpin,
|
2018-12-03 07:13:28 -06:00
|
|
|
{
|
|
|
|
type Item = G::Yield;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2020-01-25 13:03:10 -06:00
|
|
|
match Pin::new(&mut self.0).resume(()) {
|
2018-10-04 13:49:38 -05:00
|
|
|
Yielded(y) => Some(y),
|
|
|
|
_ => None
|
2018-12-03 07:13:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bug<'a>() -> impl Iterator<Item = &'a str> {
|
|
|
|
GenIter(move || {
|
|
|
|
let mut s = String::new();
|
2019-04-22 12:20:19 -05:00
|
|
|
yield &s[..] //~ ERROR cannot yield value referencing local variable `s` [E0515]
|
2019-04-22 02:40:08 -05:00
|
|
|
//~| ERROR borrow may still be in use when generator yields
|
2018-12-03 07:13:28 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
bug();
|
|
|
|
}
|