From 936e3ceddb6eeeb534a75eff86f3ba7a3833b6b5 Mon Sep 17 00:00:00 2001 From: Christopher Skane Date: Wed, 22 Nov 2023 19:25:16 -0500 Subject: [PATCH] Fixed example code for `Flock::unlock()`. --- src/fcntl.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/fcntl.rs b/src/fcntl.rs index 3ed75d20b5..2d70770900 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -707,7 +707,7 @@ impl Flock { /// # use std::fs::File; /// # use nix::fcntl::{Flock, FlockArg}; /// fn do_stuff(file: File) -> nix::Result<()> { - /// let lock = match Flock::lock(file, FlockArg::LockExclusive) { + /// let mut lock = match Flock::lock(file, FlockArg::LockExclusive) { /// Ok(l) => l, /// Err((_,e)) => return Err(e), /// }; @@ -716,26 +716,25 @@ impl Flock { /// /// // Unlock (don't continue until unlocked) /// let file = loop { - /// if let Ok(f) = lock.unlock(false) { - /// break f - /// } + /// lock = match lock.unlock(false) { + /// Ok(f) => break f, + /// Err((l,_)) => l, + /// }; /// }; /// /// // Do anything else /// /// Ok(()) /// } - pub fn unlock(self, nonblock: bool) -> Result { + pub fn unlock(self, nonblock: bool) -> std::result::Result { let flag = match nonblock { true => libc::LOCK_UN | libc::LOCK_NB, false => libc::LOCK_UN, }; - let inner = unsafe { - match Errno::result(libc::flock(self.0.as_raw_fd(), flag)) { - Ok(_) => std::ptr::read(&self.0), - Err(errno) => return Err(errno), - } - }; + let inner = unsafe { match Errno::result(libc::flock(self.0.as_raw_fd(), flag)) { + Ok(_) => std::ptr::read(&self.0), + Err(errno) => return Err((self, errno)), + }}; std::mem::forget(self); Ok(inner)