| 1 | module main |
| 2 | |
| 3 | const fixed_array_size = 4 |
| 4 | |
| 5 | fn forward_threads(threads [fixed_array_size]thread) thread { |
| 6 | return threads[0] |
| 7 | } |
| 8 | |
| 9 | fn worker_thread() {} |
| 10 | |
| 11 | fn test_mut_closure_capture_can_update_fixed_array_elements() { |
| 12 | mut values := [fixed_array_size]int{} |
| 13 | update := fn [mut values] () { |
| 14 | values[0] = 42 |
| 15 | values[3] = 7 |
| 16 | } |
| 17 | update() |
| 18 | assert values[0] == 42 |
| 19 | assert values[3] == 7 |
| 20 | } |
| 21 | |
| 22 | fn test_mut_closure_capture_of_fixed_array_sees_late_updates() { |
| 23 | mut values := [fixed_array_size]int{} |
| 24 | read := fn [mut values] () int { |
| 25 | return values[0] |
| 26 | } |
| 27 | values[0] = 99 |
| 28 | assert read() == 99 |
| 29 | } |
| 30 | |
| 31 | fn test_mut_closure_capture_of_fixed_array_of_threads_sees_late_updates() { |
| 32 | mut threads := [fixed_array_size]thread{} |
| 33 | read := fn [mut threads] () thread { |
| 34 | return forward_threads(threads) |
| 35 | } |
| 36 | threads[0] = spawn worker_thread() |
| 37 | assert read() == threads[0] |
| 38 | threads[0].wait() |
| 39 | } |
| 40 | |