1 #![feature(generators, generator_trait, never_type)]
4 use std::mem::ManuallyDrop;
7 GeneratorState::{self, *},
11 use std::sync::atomic::{AtomicUsize, Ordering};
14 fn finish<T>(mut amt: usize, mut t: T) -> T::Return
16 T: Generator<Yield = usize>,
18 // We are not moving the `t` around until it gets dropped, so this is okay.
19 let mut t = unsafe { Pin::new_unchecked(&mut t) };
21 let state = t.as_mut().resume(());
22 // Test if the generator is valid (according to type invariants).
23 let _ = unsafe { ManuallyDrop::new(ptr::read(t.as_mut().get_unchecked_mut())) };
25 GeneratorState::Yielded(y) => {
28 GeneratorState::Complete(ret) => {
41 finish(1, || yield 1);
53 finish(7 * 8 / 2, || {
84 // also test a self-referential generator
87 let mut x = Box::new(5);
105 #[allow(unreachable_code)]
112 #[allow(unreachable_code)]
113 let _x: (String, !) = (String::new(), {
120 fn smoke_resume_arg() {
121 fn drain<G: Generator<R, Yield = Y> + Unpin, R, Y>(
123 inout: Vec<(R, GeneratorState<Y, G::Return>)>,
125 Y: Debug + PartialEq,
126 G::Return: Debug + PartialEq,
128 let mut gen = Pin::new(gen);
130 for (input, out) in inout {
131 assert_eq!(gen.as_mut().resume(input), out);
132 // Test if the generator is valid (according to type invariants).
133 let _ = unsafe { ManuallyDrop::new(ptr::read(gen.as_mut().get_unchecked_mut())) };
137 static DROPS: AtomicUsize = AtomicUsize::new(0);
139 #[derive(Debug, PartialEq)]
142 impl Drop for DropMe {
144 DROPS.fetch_add(1, Ordering::SeqCst);
148 fn expect_drops<T>(expected_drops: usize, f: impl FnOnce() -> T) -> T {
149 DROPS.store(0, Ordering::SeqCst);
153 let actual_drops = DROPS.load(Ordering::SeqCst);
154 assert_eq!(actual_drops, expected_drops);
165 vec![(1, Yielded(2)), (-45, Yielded(-44)), (500, Yielded(501)), (0, Complete(-1))],
168 expect_drops(2, || drain(&mut |a| yield a, vec![(DropMe, Yielded(DropMe))]));
172 &mut |a| yield yield a,
173 vec![(DropMe, Yielded(DropMe)), (DropMe, Yielded(DropMe)), (DropMe, Complete(DropMe))],
177 #[allow(unreachable_code)]
178 expect_drops(2, || drain(&mut |a| yield return a, vec![(DropMe, Complete(DropMe))]));
183 if false { yield () } else { a }
185 vec![(DropMe, Complete(DropMe))],
191 #[allow(unused_assignments, unused_variables)]
192 &mut |mut a: DropMe| {
198 (DropMe, Yielded(())),
199 (DropMe, Yielded(())),
200 (DropMe, Yielded(())),
201 (DropMe, Complete(())),