9 releases (4 stable)

1.2.0 May 18, 2024
1.1.8 Aug 23, 2019
1.1.6 Apr 5, 2019
0.1.4 Mar 3, 2019
0.1.1 Jul 20, 2018

#29 in Development tools

Download history 6691/week @ 2024-07-20 6018/week @ 2024-07-27 5144/week @ 2024-08-03 6296/week @ 2024-08-10 5166/week @ 2024-08-17 5677/week @ 2024-08-24 7311/week @ 2024-08-31 6960/week @ 2024-09-07 5053/week @ 2024-09-14 6597/week @ 2024-09-21 5956/week @ 2024-09-28 6008/week @ 2024-10-05 5490/week @ 2024-10-12 4806/week @ 2024-10-19 5217/week @ 2024-10-26 4654/week @ 2024-11-02

21,143 downloads per month
Used in 64 crates (13 directly)

MIT/Apache

23KB
273 lines

[enclose]

(A convenient macro, for cloning values into a closure.)

Usage

Add this to your Cargo.toml:

[dependencies]
enclose = "1.2.0"

and this to your source code:

use enclose::enclose;

Example

EasyUse

Just use it!

use enclose::enclose;

fn main() {
	let clone_data = 0;
	let add_data = 100;
	
	my_enclose( enclose!((mut clone_data, add_data) || {
		// (mut clone_data, add_data) ->
		// let mut clone_data = clone_data.clone();
		// let add_data = add_data.clone();
		
		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<F: FnOnce() -> R, R>(a: F) -> R {
	a()
}

MutexUse

Creating closures for a multi-threaded environment, no extra lines!

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

use enclose::enclose;

fn main() {
	let mutex_data = Arc::new(Mutex::new( 0 ));
	let thread = thread::spawn( enclose!((mutex_data => d) move || {
		// (mutex_data => d) ->
		// let d = mutex_data.clone();
		
		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);
	}
}

ArcMutexUse

A more complex example of using an enclose macro in a multi-threaded environment.

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 = data1.clone();
				// let data2 = 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));
	}
}

EasyCopy

Using copy instead of clone.

use enclose::enclose;
use std::sync::Arc;

fn main() {
	let clone_data = Arc::new(0);
	let add_data = Arc::new(100);
	
	my_enclose( enclose!((mut *clone_data, *add_data) || {
		// (mut *clone_data, *add_data)
		// let mut clone_data = *clone_data;
		// let add_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<F: FnOnce() -> R, R>(a: F) -> R {
	a()
}
See all

License

This project has a dual license according to (LICENSE-MIT) and (LICENSE-APACHE-2-0).

uproject  Copyright (c) 2019-2024 #UlinProject

 (Denis Kotlyarov).


Apache License

apache2  Licensed under the Apache License, Version 2.0.



MIT License

mit  Licensed under the MIT License.



No runtime deps