13 stable releases

1.1.11 Mar 19, 2022
1.1.10 Apr 22, 2021
1.1.9 Jul 29, 2020
1.1.7 Sep 17, 2019
1.1.1 Nov 14, 2018

#237 in Asynchronous

Download history 650/week @ 2024-08-24 592/week @ 2024-08-31 663/week @ 2024-09-07 427/week @ 2024-09-14 800/week @ 2024-09-21 818/week @ 2024-09-28 892/week @ 2024-10-05 941/week @ 2024-10-12 1372/week @ 2024-10-19 741/week @ 2024-10-26 786/week @ 2024-11-02 670/week @ 2024-11-09 1042/week @ 2024-11-16 653/week @ 2024-11-23 788/week @ 2024-11-30 483/week @ 2024-12-07

3,111 downloads per month
Used in 5 crates (3 directly)

MIT license

7KB
73 lines

Synchronized Writer

CI

A tiny implement for synchronously writing data.

Examples

SynchronizedWriter

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

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