use std::sync::atomic::Ordering::Relaxed;
use std::sync::{atomic::AtomicUsize, Arc};
use std::time::Duration;
use capture_it::capture;
fn main() {
let va = Arc::new(AtomicUsize::new(0));
std::thread::spawn(capture!([va], move || {
va.fetch_add(1, Relaxed);
}));
std::thread::spawn(capture!([va_other = va.clone()], move || {
va_other.fetch_add(1, Relaxed);
}));
std::thread::spawn(capture!([*va], move || {
va = Arc::new(AtomicUsize::new(0));
va.fetch_add(1, Relaxed);
}));
while capture!([&va], move || -> bool {
if va.load(Relaxed) == 2 {
false
} else {
std::thread::sleep(Duration::from_millis(10));
true
}
})() {}
println!("va = {}", va.load(Relaxed));
assert_eq!(va.load(Relaxed), 2);
let _ = capture!([], move || va);
let _ = capture!([], move || <Vec<u8>>::new());
#[allow(unused_unsafe)]
let _ = capture!([], move || unsafe { <Vec<u8>>::new() });
#[derive(Default)]
struct Foo {
va: usize,
}
assert!(capture!([], move || Foo { va: 5 })().va == 5);
assert!(capture!([va = 5], move || Foo { va })().va == 5);
assert!(capture!([], move || Foo::default())().va == 0);
}