pub struct FileLock { /* private fields */ }Expand description
File lock guard that automatically releases the lock when dropped
Implementations§
Source§impl FileLock
impl FileLock
Sourcepub fn lock(file: File, lock_type: LockType) -> Result<Self>
pub fn lock(file: File, lock_type: LockType) -> Result<Self>
Acquire a file lock (blocking)
This will block until the lock can be acquired.
§Platform Support
- Unix: Uses
flock(2) - Windows: Uses
LockFile/LockFileEx
§Examples
ⓘ
use mmap_rs::{FileLock, LockType};
use std::fs::File;
let file = File::open("data.bin")?;
let lock = FileLock::lock(file, LockType::Shared)?;
// File is locked for reading
// Lock automatically released when `lock` is droppedSourcepub fn try_lock(file: File, lock_type: LockType) -> Result<Self>
pub fn try_lock(file: File, lock_type: LockType) -> Result<Self>
Try to acquire a file lock (non-blocking)
Returns immediately if the lock cannot be acquired.
§Examples
ⓘ
use mmap_rs::{FileLock, LockType};
use std::fs::File;
let file = File::open("data.bin")?;
match FileLock::try_lock(file, LockType::Exclusive) {
Ok(lock) => {
// Got the lock
}
Err(_) => {
// Lock not available
}
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for FileLock
impl RefUnwindSafe 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
Mutably borrows from an owned value. Read more