| 1 | <div id="header" align="center"> |
| 2 | |
| 3 | <b>[enclose]</b> |
| 4 | |
| 5 | (A convenient macro, for cloning values into a closure.) |
| 6 | </br></br> |
| 7 | |
| 8 | <div id="badges"> |
| 9 | <a href="./LICENSE_MIT"> |
| 10 | <img src="https://github.com/UlinProject/img/blob/main/short_32/mit.png?raw=true" alt="mit"/> |
| 11 | </a> |
| 12 | <a href="./LICENSE_APACHE"> |
| 13 | <img src="https://github.com/UlinProject/img/blob/main/short_32/apache2.png?raw=true" alt="apache2"/> |
| 14 | </a> |
| 15 | <a href="https://crates.io/crates/enclose"> |
| 16 | <img src="https://github.com/UlinProject/img/blob/main/short_32/cratesio.png?raw=true" alt="cratesio"/> |
| 17 | </a> |
| 18 | <a href="https://docs.rs/enclose"> |
| 19 | <img src="https://github.com/UlinProject/img/blob/main/short_32/docrs.png?raw=true" alt="docrs"/> |
| 20 | </a> |
| 21 | <a href="https://github.com/denisandroid"> |
| 22 | <img src="https://github.com/UlinProject/img/blob/main/short_32/uproject.png?raw=true" alt="uproject"/> |
| 23 | </a> |
| 24 | <a href="https://github.com/clucompany"> |
| 25 | <img src="https://github.com/UlinProject/img/blob/main/short_32/clulab.png?raw=true" alt="clulab"/> |
| 26 | </a> |
| 27 | |
| 28 | [](https://github.com/clucompany/enclose/actions/workflows/CI.yml) |
| 29 | |
| 30 | |
| 31 | </div> |
| 32 | </div> |
| 33 | |
| 34 | ## Usage |
| 35 | |
| 36 | Add this to your Cargo.toml: |
| 37 | |
| 38 | ```toml |
| 39 | [dependencies] |
| 40 | enclose = "1.2.1" |
| 41 | ``` |
| 42 | |
| 43 | and this to your source code: |
| 44 | |
| 45 | ```rust |
| 46 | use enclose::enclose; |
| 47 | ``` |
| 48 | |
| 49 | ## Example |
| 50 | |
| 51 | ### EasyUse |
| 52 | |
| 53 | Just use it! |
| 54 | |
| 55 | ```rust |
| 56 | use enclose::enclose; |
| 57 | |
| 58 | fn main() { |
| 59 | let clone_data = 0; |
| 60 | let add_data = 100; |
| 61 | |
| 62 | my_enclose( enclose!((mut clone_data, add_data) || { |
| 63 | // (mut clone_data, add_data) -> |
| 64 | // let mut clone_data = clone_data.clone(); |
| 65 | // let add_data = add_data.clone(); |
| 66 | |
| 67 | println!("#0 {:?}", clone_data); |
| 68 | clone_data += add_data; |
| 69 | println!("#1 {:?}", clone_data); |
| 70 | |
| 71 | assert_eq!(clone_data, 100); |
| 72 | })); |
| 73 | |
| 74 | assert_eq!(clone_data, 0); |
| 75 | } |
| 76 | |
| 77 | fn my_enclose<F: FnOnce() -> R, R>(a: F) -> R { |
| 78 | a() |
| 79 | } |
| 80 | ``` |
| 81 | |
| 82 | ### MutexUse |
| 83 | |
| 84 | Creating closures for a multi-threaded environment, no extra lines! |
| 85 | |
| 86 | ```rust |
| 87 | use std::sync::Arc; |
| 88 | use std::sync::Mutex; |
| 89 | use std::thread; |
| 90 | |
| 91 | use enclose::enclose; |
| 92 | |
| 93 | fn main() { |
| 94 | let mutex_data = Arc::new(Mutex::new( 0 )); |
| 95 | let thread = thread::spawn( enclose!((mutex_data => d) move || { |
| 96 | // (mutex_data => d) -> |
| 97 | // let d = mutex_data.clone(); |
| 98 | |
| 99 | let mut lock = match d.lock() { |
| 100 | Ok(a) => a, |
| 101 | Err(e) => e.into_inner(), |
| 102 | }; |
| 103 | *lock += 1; |
| 104 | })); |
| 105 | |
| 106 | thread.join().unwrap(); |
| 107 | { |
| 108 | let lock = match mutex_data.lock() { |
| 109 | Ok(a) => a, |
| 110 | Err(e) => e.into_inner(), |
| 111 | }; |
| 112 | assert_eq!(*lock, 1); |
| 113 | } |
| 114 | } |
| 115 | ``` |
| 116 | |
| 117 | ### ArcMutexUse |
| 118 | |
| 119 | A more complex example of using an enclose macro in a multi-threaded environment. |
| 120 | |
| 121 | ```rust |
| 122 | use std::sync::Arc; |
| 123 | use std::sync::Mutex; |
| 124 | use std::sync::RwLock; |
| 125 | use std::thread; |
| 126 | |
| 127 | use enclose::enclose; |
| 128 | |
| 129 | fn main() { |
| 130 | let data1 = Arc::new(Mutex::new( 0 )); |
| 131 | let data2 = Arc::new(RwLock::new( (0, 2, 3, 4) )); |
| 132 | |
| 133 | let count_thread = 5; |
| 134 | let mut waits = Vec::with_capacity(count_thread); |
| 135 | |
| 136 | for _a in 0..count_thread { |
| 137 | waits.push({ |
| 138 | thread::spawn( enclose!((data1, data2) move || { |
| 139 | // (data1, data2) -> |
| 140 | // let data1 = data1.clone(); |
| 141 | // let data2 = data2.clone(); |
| 142 | |
| 143 | let mut v_lock = match data1.lock() { |
| 144 | Ok(a) => a, |
| 145 | Err(e) => e.into_inner(), |
| 146 | }; |
| 147 | *v_lock += 1; |
| 148 | |
| 149 | drop( data2 ); // ignore warning |
| 150 | })) |
| 151 | }); |
| 152 | } |
| 153 | for a in waits { |
| 154 | a.join().unwrap(); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | { |
| 159 | // Check data1_lock |
| 160 | let data1_lock = match data1.lock() { |
| 161 | Ok(a) => a, |
| 162 | Err(e) => e.into_inner(), |
| 163 | }; |
| 164 | assert_eq!(*data1_lock, 5); |
| 165 | } |
| 166 | |
| 167 | { |
| 168 | // Check data2_lock |
| 169 | let data2_lock = match data2.write() { |
| 170 | Ok(a) => a, |
| 171 | Err(e) => e.into_inner(), |
| 172 | }; |
| 173 | assert_eq!(*data2_lock, (0, 2, 3, 4)); |
| 174 | } |
| 175 | } |
| 176 | ``` |
| 177 | |
| 178 | ### EasyCopy |
| 179 | |
| 180 | Using copy instead of clone. |
| 181 | |
| 182 | ```rust |
| 183 | use enclose::enclose; |
| 184 | use std::sync::Arc; |
| 185 | |
| 186 | fn main() { |
| 187 | let clone_data = Arc::new(0); |
| 188 | let add_data = Arc::new(100); |
| 189 | |
| 190 | my_enclose( enclose!((mut *clone_data, *add_data) || { |
| 191 | // (mut *clone_data, *add_data) |
| 192 | // let mut clone_data = *clone_data; |
| 193 | // let add_data = *add_data; |
| 194 | |
| 195 | println!("#0 {:?}", clone_data); |
| 196 | clone_data += add_data; |
| 197 | println!("#1 {:?}", clone_data); |
| 198 | |
| 199 | assert_eq!(clone_data, 100); |
| 200 | })); |
| 201 | |
| 202 | assert_eq!(*clone_data, 0); |
| 203 | } |
| 204 | |
| 205 | fn my_enclose<F: FnOnce() -> R, R>(a: F) -> R { |
| 206 | a() |
| 207 | } |
| 208 | ``` |
| 209 | |
| 210 | <a href="./examples"> |
| 211 | See all |
| 212 | </a> |
| 213 | |
| 214 | ## License |
| 215 | |
| 216 | This project has a dual license according to (LICENSE-MIT) and (LICENSE-APACHE-2-0). |
| 217 | |
| 218 | <div align="left"> |
| 219 | <a href="https://github.com/denisandroid"> |
| 220 | <img align="left" src="https://github.com/UlinProject/img/blob/main/block_220_100/uproject.png?raw=true" alt="uproject"/> |
| 221 | </a> |
| 222 | <b> Copyright (c) 2019-2025 #UlinProject</b> |
| 223 | |
| 224 | <b> (Denis Kotlyarov).</b> |
| 225 | </br></br></br> |
| 226 | </div> |
| 227 | |
| 228 | ### Apache License |
| 229 | |
| 230 | <div align="left"> |
| 231 | <a href="./LICENSE_APACHE"> |
| 232 | <img align="left" src="https://github.com/UlinProject/img/blob/main/block_220_100/apache2.png?raw=true" alt="apache2"/> |
| 233 | |
| 234 | </a> |
| 235 | <b> Licensed under the Apache License, Version 2.0.</b> |
| 236 | </br></br></br></br> |
| 237 | </div> |
| 238 | |
| 239 | ### MIT License |
| 240 | |
| 241 | <div align="left"> |
| 242 | <a href="./LICENSE_MIT"> |
| 243 | <img align="left" src="https://github.com/UlinProject/img/blob/main/block_220_100/mit.png?raw=true" alt="mit"/> |
| 244 | </a> |
| 245 | <b> Licensed under the MIT License.</b> |
| 246 | </br></br></br></br> |
| 247 | </div> |