]> git.lizzy.rs Git - rust.git/blob - src/test/ui/functions-closures/nullable-pointer-opt-closures.rs
Merge commit 'cd4810de42c57b64b74dae09c530a4c3a41f87b9' into libgccjit-codegen
[rust.git] / src / test / ui / functions-closures / nullable-pointer-opt-closures.rs
1 // run-pass
2
3 use std::mem;
4
5 pub fn main() {
6     // By Ref Capture
7     let a = 10i32;
8     let b = Some(|| println!("{}", a));
9     // When we capture by reference we can use any of the
10     // captures as the discriminant since they're all
11     // behind a pointer.
12     assert_eq!(mem::size_of_val(&b), mem::size_of::<usize>());
13
14     // By Value Capture
15     let a = Box::new(12i32);
16     let b = Some(move || println!("{}", a));
17     // We captured `a` by value and since it's a `Box` we can use it
18     // as the discriminant.
19     assert_eq!(mem::size_of_val(&b), mem::size_of::<Box<i32>>());
20
21     // By Value Capture - Transitive case
22     let a = "Hello".to_string(); // String -> Vec -> Unique -> NonZero
23     let b = Some(move || println!("{}", a));
24     // We captured `a` by value and since down the chain it contains
25     // a `NonZero` field, we can use it as the discriminant.
26     assert_eq!(mem::size_of_val(&b), mem::size_of::<String>());
27
28     // By Value - No Optimization
29     let a = 14i32;
30     let b = Some(move || println!("{}", a));
31     // We captured `a` by value but we can't use it as the discriminant
32     // thus we end up with an extra field for the discriminant
33     assert_eq!(mem::size_of_val(&b), mem::size_of::<(i32, i32)>());
34 }