Enclose

A convenient macro for cloning values into a closure.

Build Status Apache licensed crates.io Documentation

Use

```rust use enclose::enclose;

fn main() { let clonedata = 0; let adddata = 100;

my_enclose( enclose!((mut clone_data, add_data) || {
    println!("#0 {:?}", clone_data);
    clone_data += add_data;
    println!("#1 {:?}", clone_data);

    assert_eq!(clone_data, 100);
}));

assert_eq!(clone_data, 0);

}

fn my_enclose R, R>(a: F) -> R { a() } ```

Use 1

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

use enclose::enclose;

fn main() { let mutexdata = Arc::new(Mutex::new( 0 )); let thread = thread::spawn( enclose!((mutexdata => d) move || { let mut lock = match d.lock() { Ok(a) => a, Err(e) => e.into_inner(), }; *lock += 1; }));

thread.join().unwrap();
{
    let lock = match mutex_data.lock() {
        Ok(a) => a,
        Err(e) => e.into_inner(),
    };
    assert_eq!(*lock, 1);
}

} ```

Use 2

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

use enclose::enclose;

fn main() { let data1 = Arc::new(Mutex::new( 0 )); let data2 = Arc::new(RwLock::new( (0, 2, 3, 4) ));

let count_thread = 5;
let mut waits = Vec::with_capacity(count_thread);

for _a in 0..count_thread {
    waits.push({
        thread::spawn( enclose!((data1, data2) move || {
            //(data1, data2) -> 
            //let data1 = 'root.data1.clone();
            //let data2 = 'root.data2.clone();

            let mut v_lock = match data1.lock() {
                Ok(a) => a,
                Err(e) => e.into_inner(),
            };
            *v_lock += 1;

            drop( data2 ); //ignore warning
        }))
    });
}
for a in waits {
    a.join().unwrap();
}


{   
    //Check data1_lock
    let data1_lock = match data1.lock() {
        Ok(a) => a,
        Err(e) => e.into_inner(),
    };
    assert_eq!(*data1_lock, 5);
}

{   
    //Check data2_lock
    let data2_lock = match data2.write() {
        Ok(a) => a,
        Err(e) => e.into_inner(),
    };
    assert_eq!(*data2_lock, (0, 2, 3, 4));
}

} ```

Use 3

```rust use enclose::enclose; use std::sync::Arc;

fn main() { let clonedata = Arc::new(0); let adddata = Arc::new(100);

my_enclose( enclose!((mut *clone_data, *add_data) || {
    println!("#0 {:?}", clone_data);
    clone_data += add_data;
    println!("#1 {:?}", clone_data);

    assert_eq!(clone_data, 100);
}));

assert_eq!(*clone_data, 0);

}

fn my_enclose R, R>(a: F) -> R { a() } ```

License

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

Licensed under the MIT License