Struct nix::fcntl::Flock

source ·
pub struct Flock<T: Flockable>(/* private fields */);
Available on crate feature fs only.
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>

source

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();
source

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(())
}
source

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§

source§

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

source§

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

Formats the value using the given formatter. Read more
source§

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

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

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

source§

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

Mutably dereferences the value.
source§

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

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

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

source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.