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.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for FileLock
impl RefUnwindSafe for FileLock
impl Send for FileLock
impl Sync for FileLock
impl Unpin for FileLock
impl UnwindSafe for FileLock
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Erasable for T
impl<T> Erasable for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more