#arc #sync #write #mutex #synchronized

synchronized-writer

A tiny implement for synchronously writing data

12 stable releases

1.1.10 Apr 22, 2021
1.1.9 Jul 29, 2020
1.1.8 Jun 27, 2020
1.1.7 Sep 17, 2019
1.1.1 Nov 14, 2018

#102 in Asynchronous

Download history 287/week @ 2021-08-11 211/week @ 2021-08-18 199/week @ 2021-08-25 101/week @ 2021-09-01 240/week @ 2021-09-08 251/week @ 2021-09-15 145/week @ 2021-09-22 228/week @ 2021-09-29 288/week @ 2021-10-06 279/week @ 2021-10-13 4/week @ 2021-10-20 8/week @ 2021-10-27 31/week @ 2021-11-03 21/week @ 2021-11-10 135/week @ 2021-11-17 17/week @ 2021-11-24

207 downloads per month

MIT license

7KB
73 lines

Synchronized Writer

CI

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