Enclose

A convenient macro for cloning values into a closure.

Build Status Apache licensed crates.io Documentation

Use

``` use std::thread; use std::sync::Arc; use std::sync::Mutex;

use enclose::enclose;

let v = Arc::new(Mutex::new( 0 )); let thread = thread::spawn( enclose!((v) move || { let mut vlock = match v.lock() { Ok(a) => a, Err(e) => e.intoinner(), }; *v_lock += 1; }));

thread.join().unwrap(); { let vlock = match v.lock() { Ok(a) => a, Err(e) => e.intoinner(), }; asserteq!(*vlock, 1); } ```

Use 2

``` use std::thread; use std::sync::Arc; use std::sync::Mutex; use std::sync::RwLock;

use enclose::enclose;

let v = Arc::new(Mutex::new( 0 )); let v2 = Arc::new(RwLock::new( (0, 2, 3, 4) ));

let countthread = 5; let mut waitall = Vec::withcapacity(countthread);

for a in 0..countthread { waitall.push({ thread::spawn( enclose!((v, v2) move || { let mut vlock = match v.lock() { Ok(a) => a, Err(e) => e.intoinner(), }; *vlock += 1;

        drop( v2 ); //ignore warning
    }))
});

} for a in waitall { a.join().unwrap(); } {
//Test result let v
lock = match v.lock() { Ok(a) => a, Err(e) => e.intoinner(), }; asserteq!(*v_lock, 5); } ```

Use 3

``` use std::sync::Arc; use std::sync::Mutex; use std::thread;

use enclose::enclose;

let v = Arc::new(Mutex::new( 0 )); let thread = thread::spawn( enclose!((v => MYLOCKER) move || { let mut vlock = match MYLOCKER.lock() { Ok(a) => a, Err(e) => e.intoinner(), }; *v_lock += 1; }));

thread.join().unwrap(); { let vlock = match v.lock() { Ok(a) => a, Err(e) => e.intoinner(), }; asserteq!(*vlock, 1); } ```

License

Copyright 2019 #UlinProject (Denis Kotlyarov) Денис Котляров

Licensed under the MIT License