yoshuawuyts / fd-lock

Advisory cross-platform file locks using file descriptors
Apache License 2.0
76 stars 18 forks source link

Rwlock #13

Closed yoshuawuyts closed 3 years ago

yoshuawuyts commented 3 years ago

Move from a Mutex-inspired API to an RwLock inspired API. Closes #11.

use std::io::prelude::*;
use std::fs::File;
use fd_lock::RwLock;

// Lock a file and write to it.
let mut f = RwLock::new(File::open("foo.txt")?);
write!(f.write()?, "chashu cat")?;

// A lock can also be held across multiple operations.
let mut f = f.write()?;
write!(f, "nori cat")?;
write!(f, "bird!")?;