Skip to content

Commit

Permalink
1. Fix cargo test: doc, use, ...
Browse files Browse the repository at this point in the history
  • Loading branch information
denisandroid committed Mar 3, 2019
1 parent 19573ba commit a3ca627
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 102 deletions.
163 changes: 87 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,103 @@ A convenient macro for cloning values into a closure.
[![crates.io](http://meritbadge.herokuapp.com/enclose)](https://crates.io/crates/enclose)
[![Documentation](https://docs.rs/enclose/badge.svg)](https://docs.rs/enclose)


# 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 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);
}
```

use std::thread;
use std::sync::Arc;
use std::sync::Mutex;
# Use 2
```
use std::thread;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;
let v = Arc::new(Mutex::new( 0 ));
let thread = thread::spawn( enclose!((v) move || {
let mut v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
};
*v_lock += 1;
}));
use enclose::enclose;
thread.join().unwrap();
{
let v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
};
assert_eq!(*v_lock, 1);
}
# Use 2
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);
use std::thread;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;

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( enclose!((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(),
for _a in 0..count_thread {
wait_all.push({
thread::spawn( enclose!((v, v2) move || {
let mut v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
};
assert_eq!(*v_lock, 5);
}
*v_lock += 1;
# Use 3
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);
}
```

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);
}
# 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 => 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

Expand Down
4 changes: 1 addition & 3 deletions examples/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ 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 thread = thread::spawn( enclose!((v) move || {
let mut v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
Expand Down
17 changes: 8 additions & 9 deletions examples/easy2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ 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;
}));
let v = Arc::new(Mutex::new( 0 ));
let thread = thread::spawn( enclose!((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();
{
Expand Down
4 changes: 1 addition & 3 deletions examples/easy3.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@


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

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

fn main() {
Expand All @@ -18,7 +16,7 @@ fn main() {

for _a in 0..count_thread {
wait_all.push({
thread::spawn( enc!((v, v2) move || {
thread::spawn( enclose!((v, v2) move || {
let mut v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
Expand Down
29 changes: 18 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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 v_lock = match v.lock() {
Expand All @@ -53,6 +55,9 @@ 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) ));
Expand Down Expand Up @@ -92,13 +97,15 @@ 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( enc!((v => MY_LOCKER) move || {
let mut v_lock = match MY_LOCKER.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
};
*v_lock += 1;
let thread = thread::spawn( enclose!((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();
Expand Down Expand Up @@ -135,8 +142,8 @@ macro_rules! enclose {
///Macro for cloning values to close. Alternative short record.
#[macro_export]
macro_rules! enc {
($($arg:tt)*) => {
enclose!( $($arg)* )
($($tt:tt)*) => {
enclose!( $($tt)* )
};
}

Expand All @@ -151,7 +158,7 @@ mod tests {
#[test]
fn easy() {
let v = Arc::new(Mutex::new( 0 ));
let thread = thread::spawn( enc!((v) move || {
let thread = thread::spawn( enclose!((v) move || {
let mut v_lock = match v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
Expand All @@ -171,7 +178,7 @@ mod tests {
#[test]
fn easy_extract() {
let v = Arc::new(Mutex::new( 0 ));
let thread = thread::spawn( enc!((v => my_v) move || {
let thread = thread::spawn( enclose!((v => my_v) move || {
let mut v_lock = match my_v.lock() {
Ok(a) => a,
Err(e) => e.into_inner(),
Expand Down Expand Up @@ -199,7 +206,7 @@ mod tests {

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

0 comments on commit a3ca627

Please sign in to comment.