#arc #synchronized #sync #write #mutex

synchronized-writer

A tiny implement for synchronously writing data

13 stable releases

Uses new Rust 2021

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

#108 in Asynchronous

Download history 63/week @ 2022-10-07 155/week @ 2022-10-14 163/week @ 2022-10-21 255/week @ 2022-10-28 120/week @ 2022-11-04 160/week @ 2022-11-11 105/week @ 2022-11-18 102/week @ 2022-11-25 81/week @ 2022-12-02 122/week @ 2022-12-09 65/week @ 2022-12-16 38/week @ 2022-12-23 111/week @ 2022-12-30 213/week @ 2023-01-06 172/week @ 2023-01-13 152/week @ 2023-01-20

653 downloads per month
Used in embedded-hal-vcd

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