@@ -116,6 +116,19 @@ pub struct File {
116116 inner : fs_imp:: File ,
117117}
118118
119+ /// An enumeration of possible errors which can occur while trying to acquire a lock
120+ /// from the [`try_lock`] method and [`try_lock_shared`] method on a [`File`].
121+ ///
122+ /// [`try_lock`]: File::try_lock
123+ /// [`try_lock_shared`]: File::try_lock_shared
124+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
125+ pub enum TryLockError {
126+ /// The lock could not be acquired due to an I/O error on the file.
127+ Error ( io:: Error ) ,
128+ /// The lock could not be acquired at this time because the operation would block.
129+ WouldBlock ,
130+ }
131+
119132/// Metadata information about a file.
120133///
121134/// This structure is returned from the [`metadata`] or
@@ -352,6 +365,27 @@ pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result
352365 inner ( path. as_ref ( ) , contents. as_ref ( ) )
353366}
354367
368+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
369+ impl fmt:: Debug for TryLockError {
370+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
371+ match self {
372+ TryLockError :: Error ( err) => err. fmt ( f) ,
373+ TryLockError :: WouldBlock => "WouldBlock" . fmt ( f) ,
374+ }
375+ }
376+ }
377+
378+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
379+ impl fmt:: Display for TryLockError {
380+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
381+ match self {
382+ TryLockError :: Error ( _) => "lock acquisition failed due to I/O error" ,
383+ TryLockError :: WouldBlock => "lock acquisition failed because the operation would block" ,
384+ }
385+ . fmt ( f)
386+ }
387+ }
388+
355389impl File {
356390 /// Attempts to open a file in read-only mode.
357391 ///
@@ -734,8 +768,8 @@ impl File {
734768
735769 /// Try to acquire an exclusive lock on the file.
736770 ///
737- /// Returns `Ok(false )` if a different lock is already held on this file (via another
738- /// handle/descriptor).
771+ /// Returns `Err(TryLockError::WouldBlock )` if a different lock is already held on this file
772+ /// (via another handle/descriptor).
739773 ///
740774 /// This acquires an exclusive lock; no other file handle to this file may acquire another lock.
741775 ///
@@ -777,23 +811,27 @@ impl File {
777811 ///
778812 /// ```no_run
779813 /// #![feature(file_lock)]
780- /// use std::fs::File;
814+ /// use std::fs::{ File, TryLockError} ;
781815 ///
782816 /// fn main() -> std::io::Result<()> {
783817 /// let f = File::create("foo.txt")?;
784- /// f.try_lock()?;
818+ /// match f.try_lock() {
819+ /// Ok(_) => (),
820+ /// Err(TryLockError::WouldBlock) => (), // Lock not acquired
821+ /// Err(TryLockError::Error(err)) => return Err(err),
822+ /// }
785823 /// Ok(())
786824 /// }
787825 /// ```
788826 #[ unstable( feature = "file_lock" , issue = "130994" ) ]
789- pub fn try_lock ( & self ) -> io :: Result < bool > {
827+ pub fn try_lock ( & self ) -> Result < ( ) , TryLockError > {
790828 self . inner . try_lock ( )
791829 }
792830
793831 /// Try to acquire a shared (non-exclusive) lock on the file.
794832 ///
795- /// Returns `Ok(false )` if an exclusive lock is already held on this file (via another
796- /// handle/descriptor).
833+ /// Returns `Err(TryLockError::WouldBlock )` if a different lock is already held on this file
834+ /// (via another handle/descriptor).
797835 ///
798836 /// This acquires a shared lock; more than one file handle may hold a shared lock, but none may
799837 /// hold an exclusive lock at the same time.
@@ -834,16 +872,21 @@ impl File {
834872 ///
835873 /// ```no_run
836874 /// #![feature(file_lock)]
837- /// use std::fs::File;
875+ /// use std::fs::{ File, TryLockError} ;
838876 ///
839877 /// fn main() -> std::io::Result<()> {
840878 /// let f = File::open("foo.txt")?;
841- /// f.try_lock_shared()?;
879+ /// match f.try_lock_shared() {
880+ /// Ok(_) => (),
881+ /// Err(TryLockError::WouldBlock) => (), // Lock not acquired
882+ /// Err(TryLockError::Error(err)) => return Err(err),
883+ /// }
884+ ///
842885 /// Ok(())
843886 /// }
844887 /// ```
845888 #[ unstable( feature = "file_lock" , issue = "130994" ) ]
846- pub fn try_lock_shared ( & self ) -> io :: Result < bool > {
889+ pub fn try_lock_shared ( & self ) -> Result < ( ) , TryLockError > {
847890 self . inner . try_lock_shared ( )
848891 }
849892
0 commit comments