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