@@ -116,6 +116,22 @@ pub struct File {
116
116
inner : fs_imp:: File ,
117
117
}
118
118
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. The standard library will
127
+ /// not return an [`ErrorKind::WouldBlock`] error inside [`TryLockError::Error`]
128
+ ///
129
+ /// [`ErrorKind::WouldBlock`]: io::ErrorKind::WouldBlock
130
+ Error ( io:: Error ) ,
131
+ /// The lock could not be acquired at this time because it is held by another handle/process.
132
+ WouldBlock ,
133
+ }
134
+
119
135
/// Metadata information about a file.
120
136
///
121
137
/// This structure is returned from the [`metadata`] or
@@ -352,6 +368,27 @@ pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result
352
368
inner ( path. as_ref ( ) , contents. as_ref ( ) )
353
369
}
354
370
371
+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
372
+ impl fmt:: Debug for TryLockError {
373
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
374
+ match self {
375
+ TryLockError :: Error ( err) => err. fmt ( f) ,
376
+ TryLockError :: WouldBlock => "WouldBlock" . fmt ( f) ,
377
+ }
378
+ }
379
+ }
380
+
381
+ #[ unstable( feature = "file_lock" , issue = "130994" ) ]
382
+ impl fmt:: Display for TryLockError {
383
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
384
+ match self {
385
+ TryLockError :: Error ( _) => "lock acquisition failed due to I/O error" ,
386
+ TryLockError :: WouldBlock => "lock acquisition failed because the operation would block" ,
387
+ }
388
+ . fmt ( f)
389
+ }
390
+ }
391
+
355
392
impl File {
356
393
/// Attempts to open a file in read-only mode.
357
394
///
@@ -734,8 +771,8 @@ impl File {
734
771
735
772
/// Try to acquire an exclusive lock on the file.
736
773
///
737
- /// Returns `Ok(false )` if a different lock is already held on this file (via another
738
- /// handle/descriptor).
774
+ /// Returns `Err(TryLockError::WouldBlock )` if a different lock is already held on this file
775
+ /// (via another handle/descriptor).
739
776
///
740
777
/// This acquires an exclusive lock; no other file handle to this file may acquire another lock.
741
778
///
@@ -777,23 +814,27 @@ impl File {
777
814
///
778
815
/// ```no_run
779
816
/// #![feature(file_lock)]
780
- /// use std::fs::File;
817
+ /// use std::fs::{ File, TryLockError} ;
781
818
///
782
819
/// fn main() -> std::io::Result<()> {
783
820
/// let f = File::create("foo.txt")?;
784
- /// f.try_lock()?;
821
+ /// match f.try_lock() {
822
+ /// Ok(_) => (),
823
+ /// Err(TryLockError::WouldBlock) => (), // Lock not acquired
824
+ /// Err(TryLockError::Error(err)) => return Err(err),
825
+ /// }
785
826
/// Ok(())
786
827
/// }
787
828
/// ```
788
829
#[ unstable( feature = "file_lock" , issue = "130994" ) ]
789
- pub fn try_lock ( & self ) -> io :: Result < bool > {
830
+ pub fn try_lock ( & self ) -> Result < ( ) , TryLockError > {
790
831
self . inner . try_lock ( )
791
832
}
792
833
793
834
/// Try to acquire a shared (non-exclusive) lock on the file.
794
835
///
795
- /// Returns `Ok(false )` if an exclusive lock is already held on this file (via another
796
- /// handle/descriptor).
836
+ /// Returns `Err(TryLockError::WouldBlock )` if a different lock is already held on this file
837
+ /// (via another handle/descriptor).
797
838
///
798
839
/// This acquires a shared lock; more than one file handle may hold a shared lock, but none may
799
840
/// hold an exclusive lock at the same time.
@@ -834,16 +875,21 @@ impl File {
834
875
///
835
876
/// ```no_run
836
877
/// #![feature(file_lock)]
837
- /// use std::fs::File;
878
+ /// use std::fs::{ File, TryLockError} ;
838
879
///
839
880
/// fn main() -> std::io::Result<()> {
840
881
/// let f = File::open("foo.txt")?;
841
- /// f.try_lock_shared()?;
882
+ /// match f.try_lock_shared() {
883
+ /// Ok(_) => (),
884
+ /// Err(TryLockError::WouldBlock) => (), // Lock not acquired
885
+ /// Err(TryLockError::Error(err)) => return Err(err),
886
+ /// }
887
+ ///
842
888
/// Ok(())
843
889
/// }
844
890
/// ```
845
891
#[ unstable( feature = "file_lock" , issue = "130994" ) ]
846
- pub fn try_lock_shared ( & self ) -> io :: Result < bool > {
892
+ pub fn try_lock_shared ( & self ) -> Result < ( ) , TryLockError > {
847
893
self . inner . try_lock_shared ( )
848
894
}
849
895
0 commit comments