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