@@ -116,6 +116,19 @@ 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.
127
+ Error ( io:: Error ) ,
128
+ /// The lock could not be acquired at this time because the operation would block.
129
+ WouldBlock ,
130
+ }
131
+
119
132
/// Metadata information about a file.
120
133
///
121
134
/// 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
352
365
inner ( path. as_ref ( ) , contents. as_ref ( ) )
353
366
}
354
367
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
+
355
389
impl File {
356
390
/// Attempts to open a file in read-only mode.
357
391
///
@@ -734,8 +768,8 @@ impl File {
734
768
735
769
/// Try to acquire an exclusive lock on the file.
736
770
///
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).
739
773
///
740
774
/// This acquires an exclusive lock; no other file handle to this file may acquire another lock.
741
775
///
@@ -777,23 +811,27 @@ impl File {
777
811
///
778
812
/// ```no_run
779
813
/// #![feature(file_lock)]
780
- /// use std::fs::File;
814
+ /// use std::fs::{ File, TryLockError} ;
781
815
///
782
816
/// fn main() -> std::io::Result<()> {
783
817
/// 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
+ /// }
785
823
/// Ok(())
786
824
/// }
787
825
/// ```
788
826
#[ unstable( feature = "file_lock" , issue = "130994" ) ]
789
- pub fn try_lock ( & self ) -> io :: Result < bool > {
827
+ pub fn try_lock ( & self ) -> Result < ( ) , TryLockError > {
790
828
self . inner . try_lock ( )
791
829
}
792
830
793
831
/// Try to acquire a shared (non-exclusive) lock on the file.
794
832
///
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).
797
835
///
798
836
/// This acquires a shared lock; more than one file handle may hold a shared lock, but none may
799
837
/// hold an exclusive lock at the same time.
@@ -834,16 +872,21 @@ impl File {
834
872
///
835
873
/// ```no_run
836
874
/// #![feature(file_lock)]
837
- /// use std::fs::File;
875
+ /// use std::fs::{ File, TryLockError} ;
838
876
///
839
877
/// fn main() -> std::io::Result<()> {
840
878
/// 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
+ ///
842
885
/// Ok(())
843
886
/// }
844
887
/// ```
845
888
#[ 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 > {
847
890
self . inner . try_lock_shared ( )
848
891
}
849
892
0 commit comments