|
| 1 | +//@ run-pass |
| 2 | + |
| 3 | +#![allow(incomplete_features)] |
| 4 | +#![feature(iter_macro, move_expr, yield_expr)] |
| 5 | + |
| 6 | +use std::cell::Cell; |
| 7 | +use std::iter::iter; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +fn main() { |
| 11 | + let created = Cell::new(0); |
| 12 | + let closure = iter! { || { |
| 13 | + let n = move({ |
| 14 | + created.set(created.get() + 1); |
| 15 | + created.get() |
| 16 | + }); |
| 17 | + yield n; |
| 18 | + }}; |
| 19 | + assert_eq!(created.get(), 1); |
| 20 | + assert_eq!(closure().next(), Some(1)); |
| 21 | + assert_eq!(closure().next(), Some(1)); |
| 22 | + assert_eq!(created.get(), 1); |
| 23 | + |
| 24 | + let x = Arc::new(String::from("hello")); |
| 25 | + assert_eq!(Arc::strong_count(&x), 1); |
| 26 | + |
| 27 | + let closure = iter! { || { |
| 28 | + yield move(x.clone()); |
| 29 | + }}; |
| 30 | + assert_eq!(Arc::strong_count(&x), 2); |
| 31 | + let mut generator = closure(); |
| 32 | + assert_eq!(Arc::strong_count(&x), 2); |
| 33 | + let yielded = generator.next().unwrap(); |
| 34 | + assert_eq!(Arc::strong_count(&x), 2); |
| 35 | + assert_eq!(generator.next(), None); |
| 36 | + drop(yielded); |
| 37 | + assert_eq!(Arc::strong_count(&x), 1); |
| 38 | + |
| 39 | + let a = String::from("a"); |
| 40 | + let b = String::from("bbb"); |
| 41 | + let closure = iter! { || { |
| 42 | + let moved = move(a.clone()); |
| 43 | + yield (moved, b.len()); |
| 44 | + }}; |
| 45 | + assert_eq!(closure().next(), Some((String::from("a"), 3))); |
| 46 | +} |
0 commit comments