pub struct FileLock { /* private fields */ }Expand description
A file lock for coordinating concurrent access to shared files.
Uses a .lock file alongside the target file for locking.
This avoids issues with locking the target file directly
(which can interfere with truncation/replacement).
Implementations§
Source§impl FileLock
impl FileLock
Sourcepub fn new(path: impl AsRef<Path>) -> Result<Self>
pub fn new(path: impl AsRef<Path>) -> Result<Self>
Creates a new file lock for the given path.
The lock file is created at {path}.lock.
The parent directory is created if it doesn’t exist.
Acquires a shared (read) lock.
Multiple processes can hold shared locks simultaneously. Blocks until the lock is available.
Tries to acquire a shared (read) lock without blocking.
Returns Ok(None) if the lock is not available.
Sourcepub fn exclusive(&self) -> Result<LockGuard>
pub fn exclusive(&self) -> Result<LockGuard>
Acquires an exclusive (write) lock.
Only one process can hold an exclusive lock. Blocks until the lock is available.
Sourcepub fn try_exclusive(&self) -> Result<Option<LockGuard>>
pub fn try_exclusive(&self) -> Result<Option<LockGuard>>
Tries to acquire an exclusive (write) lock without blocking.
Returns Ok(None) if the lock is not available.