Expand description
Advisory reader-writer locks for files.
§Notes on Advisory Locks
“advisory locks” are locks which programs must opt-in to adhere to. This means that they can be used to coordinate file access, but not prevent access. Use this to coordinate file access between multiple instances of the same program. But do not use this to prevent actors from accessing or modifying files.
§Example
use std::path::PathBuf;
use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use async_fd_lock::{LockRead, LockWrite};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("foo.txt");
// Lock it for writing.
{
let mut write_guard = File::options()
.create_new(true)
.write(true)
.truncate(true)
.open(&path).await?
.lock_write().await?;
write_guard.write(b"bongo cat").await?;
}
// Lock it for reading.
{
let mut read_guard_1 = File::open(&path).await?.lock_read().await?;
let mut read_guard_2 = File::open(&path).await?.lock_read().await?;
let byte_1 = read_guard_1.read_u8().await?;
let byte_2 = read_guard_2.read_u8().await?;
}
Re-exports§
pub use nonblocking::*;
Modules§
Structs§
- Lock
Error - RwLock
Read Guard - A shared lock on a file.
- RwLock
Write Guard - An exclusive lock on a file.
Traits§
- AsOpen
File - A trait to borrow the file descriptor from an underlying object.