Skip to content

Commit

Permalink
1. Init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
denisandroid committed Dec 9, 2018
1 parent 494d3e7 commit 3b1f966
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[package]
name = "enclose"
version = "0.1.2"
version = "0.1.3"
authors = ["Денис Котляров <denis2005991@gmail.com>"]
repository = "https://github.com/clucompany/Enclose.git"
license = "MIT"
readme = "README.md"
edition = "2018"

description = "A convenient macro for cloning values into a closure."
keywords = ["enclose", "enclose_macro", "macro", "clucompany"]
Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,32 @@ A convenient macro for cloning values into a closure.
};
assert_eq!(*v_lock, 5);
}


# Use 3

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

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

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


# License

Copyright 2018 #UlinProject Денис Котляров
Expand Down
30 changes: 30 additions & 0 deletions examples/easy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@


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

use enclose::enc;
use enclose::enclose;


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

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

27 changes: 27 additions & 0 deletions examples/easy2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

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

use enclose::enc;
use enclose::enclose;

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

thread.join().unwrap();
{
let v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
};
assert_eq!(*v_lock, 1);
}
}
43 changes: 43 additions & 0 deletions examples/easy3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@


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

use enclose::enc;
use enclose::enclose;

fn main() {

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

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

for _a in 0..count_thread {
wait_all.push({
thread::spawn( enc!((v, v2) move || {
let mut v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
};
*v_lock += 1;

drop( v2 ); //ignore warning
}))
});
}
for a in wait_all {
a.join().unwrap();
}
{
//Test result
let v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
};
assert_eq!(*v_lock, 5);
}
}
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ for a in wait_all {
}
```
# Use 3
```
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
let v = Arc::new(Mutex::new( 0 ));
let thread = thread::spawn( enc!((v => MY_LOCKER) move || {
let mut v_lock = match MY_LOCKER.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
};
*v_lock += 1;
}));
thread.join().unwrap();
{
let v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
};
assert_eq!(*v_lock, 1);
}
```
*/

///Macro for cloning values to close.
Expand Down

0 comments on commit 3b1f966

Please sign in to comment.