pub struct Flock<T: Flockable>(/* private fields */);
Expand description
Represents an owned flock, which unlocks on drop.
See flock(2) for details on locking semantics.
Implementations§
source§impl<T: Flockable> Flock<T>
impl<T: Flockable> Flock<T>
sourcepub fn lock(t: T, args: FlockArg) -> Result<Self, (T, Errno)>
pub fn lock(t: T, args: FlockArg) -> Result<Self, (T, Errno)>
Obtain a/an flock.
§Example
let mut file = match Flock::lock(file, FlockArg::LockExclusive) {
Ok(l) => l,
Err(_) => return,
};
// Do stuff
let data = "Foo bar";
_ = file.write(data.as_bytes());
_ = file.sync_data();
sourcepub fn unlock(self) -> Result<T, (Self, Errno)>
pub fn unlock(self) -> Result<T, (Self, Errno)>
Remove the lock and return the object wrapped within.
§Example
fn do_stuff(file: File) -> nix::Result<()> {
let mut lock = match Flock::lock(file, FlockArg::LockExclusive) {
Ok(l) => l,
Err((_,e)) => return Err(e),
};
// Do critical section
// Unlock
let file = match lock.unlock() {
Ok(f) => f,
Err((_, e)) => return Err(e),
};
// Do anything else
Ok(())
}
sourcepub fn relock(&self, arg: FlockArg) -> Result<()>
pub fn relock(&self, arg: FlockArg) -> Result<()>
Relock the file. This can upgrade or downgrade the lock type.
§Example
let f: std::fs::File = tempfile().unwrap();
let locked_file = Flock::lock(f, FlockArg::LockExclusive).unwrap();
// Do stuff, then downgrade the lock
locked_file.relock(FlockArg::LockShared).unwrap();
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Flock<T>where
T: Freeze,
impl<T> RefUnwindSafe for Flock<T>where
T: RefUnwindSafe,
impl<T> Send for Flock<T>where
T: Send,
impl<T> Sync for Flock<T>where
T: Sync,
impl<T> Unpin for Flock<T>where
T: Unpin,
impl<T> UnwindSafe for Flock<T>where
T: UnwindSafe,
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