Crate async_fd_lock
source ·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
.map_err(|(_, err)| err)?;
write_guard.write(b"bongo cat").await?;
}
// Lock it for reading.
{
let mut read_guard_1 = File::open(&path).await?.lock_read().await.map_err(|(_, err)| err)?;
let mut read_guard_2 = File::open(&path).await?.lock_read().await.map_err(|(_, err)| err)?;
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§
- Onwed version of
RwLockReadGuard
- Onwed version of
RwLockWriteGuard
Traits§
- A trait to borrow the file descriptor from an underlying object.