Struct Flock

pub struct Flock<T: Flockable>(/* private field */);

Represents an owned flock, which unlocks on drop.

See flock(2) for details on locking semantics.

Implementations

impl<T: Flockable> Flock<T>

fn lock(t: T, args: FlockArg) -> Result<Self, (T, Errno)>

Obtain a/an flock.

Example

# use std::io::Write;
# use std::fs::File;
# use nix::fcntl::{Flock, FlockArg};
# fn do_stuff(file: File) {
  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();
# }
fn unlock(self) -> Result<T, (Self, Errno)>

Remove the lock and return the object wrapped within.

Example

# use std::fs::File;
# use nix::fcntl::{Flock, FlockArg};
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(())
}
fn relock(&self, arg: FlockArg) -> Result<()>

Relock the file. This can upgrade or downgrade the lock type.

Example

# use std::fs::File;
# use nix::fcntl::{Flock, FlockArg};
# use tempfile::tempfile;
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

impl<T: Debug + Flockable> Debug for Flock<T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: Flockable> Deref for Flock<T>

type Target = T;
fn deref(&self) -> &Self::Target

impl<T: Flockable> DerefMut for Flock<T>

fn deref_mut(&mut self) -> &mut Self::Target

impl<T: Flockable> Drop for Flock<T>

fn drop(&mut self)

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> UnsafeUnpin for Flock<T> where T: UnsafeUnpin,

impl<T> UnwindSafe for Flock<T> where T: UnwindSafe,

Blanket Implementations

impl<P, T> Receiver for Flock<T> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

impl<T> Any for Flock<T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Flock<T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Flock<T> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for Flock<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for Flock<T> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Flock<T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Flock<T> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>