1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*!
A tiny implement for synchronously writing data.
## Examples
### SynchronizedWriter
```rust
use synchronized_writer::SynchronizedWriter;
use std::sync::{Arc, Mutex};
use std::thread;
use std::io::Write;
let data = Mutex::new(Vec::new());
let data_arc = Arc::new(data);
let mut threads = Vec::with_capacity(10);
for _ in 0..10 {
let mut writer = SynchronizedWriter::new(data_arc.clone());
let thread = thread::spawn(move || {
writer.write(b"Hello world!").unwrap();
});
threads.push(thread);
}
for thread in threads {
thread.join().unwrap();
}
assert_eq!(b"Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!".to_vec(), *data_arc.lock().unwrap());
```
### SynchronizedOptionWriter
```rust
use synchronized_writer::SynchronizedOptionWriter;
use std::sync::{Arc, Mutex};
use std::io::Write;
let data = Mutex::new(Some(Vec::new()));
let data_arc = Arc::new(data);
let mut writer = SynchronizedOptionWriter::new(data_arc.clone());
writer.write(b"Hello world!").unwrap();
writer.flush().unwrap();
let data = data_arc.lock().unwrap().take().unwrap(); // remove out the vec from arc
assert_eq!(b"Hello world!".to_vec(), data);
```
*/
mod synchronized_option_writer;
mod synchronized_writer;
pub use self::synchronized_writer::SynchronizedWriter;
pub use synchronized_option_writer::SynchronizedOptionWriter;