#arc #sync #write #mutex #synchronized

synchronized-writer

A tiny implement for synchronously writing data

11 stable releases

1.1.9 Jul 29, 2020
1.1.8 Jun 27, 2020
1.1.7 Sep 17, 2019
1.1.6 Jul 29, 2019
1.1.0 Oct 24, 2018

#73 in Asynchronous

Download history 6/week @ 2020-11-06 1/week @ 2020-11-13 1/week @ 2020-11-20 14/week @ 2020-11-27 21/week @ 2020-12-04 11/week @ 2021-01-15 9/week @ 2021-02-05 38/week @ 2021-02-12

67 downloads per month

MIT license

7KB
73 lines

Synchronized Writer

Build Status

A tiny implement for synchronously writing data.

Examples

SynchronizedWriter

extern crate synchronized_writer;

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

extern crate synchronized_writer;

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

No runtime deps