https://github.com/magiclen/synchronized-writer
A tiny implement for synchronously writing data.
https://github.com/magiclen/synchronized-writer
rust
Last synced: about 1 year ago
JSON representation
A tiny implement for synchronously writing data.
- Host: GitHub
- URL: https://github.com/magiclen/synchronized-writer
- Owner: magiclen
- License: mit
- Created: 2018-10-24T11:35:42.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-09-09T05:32:09.000Z (almost 3 years ago)
- Last Synced: 2025-03-24T17:55:18.255Z (about 1 year ago)
- Topics: rust
- Language: Rust
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Synchronized Writer
====================
[](https://github.com/magiclen/synchronized-writer/actions/workflows/ci.yml)
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);
```
## Crates.io
https://crates.io/crates/synchronized-writer
## Documentation
https://docs.rs/synchronized-writer
## License
[MIT](LICENSE)