Skip to content

Commit fd260d5

Browse files
committed
Add From<TryLockError> for io::Error
This makes error propagation from try_lock() and try_lock_shared() more convenient
1 parent 9febbf8 commit fd260d5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

library/std/src/fs.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,16 @@ impl fmt::Display for TryLockError {
392392
}
393393
}
394394

395+
#[unstable(feature = "file_lock", issue = "130994")]
396+
impl From<TryLockError> for io::Error {
397+
fn from(err: TryLockError) -> io::Error {
398+
match err {
399+
TryLockError::Error(err) => err,
400+
TryLockError::WouldBlock => io::ErrorKind::WouldBlock.into(),
401+
}
402+
}
403+
}
404+
395405
impl File {
396406
/// Attempts to open a file in read-only mode.
397407
///
@@ -821,11 +831,14 @@ impl File {
821831
///
822832
/// fn main() -> std::io::Result<()> {
823833
/// let f = File::create("foo.txt")?;
834+
/// // Explicit handling of the WouldBlock error
824835
/// match f.try_lock() {
825836
/// Ok(_) => (),
826837
/// Err(TryLockError::WouldBlock) => (), // Lock not acquired
827838
/// Err(TryLockError::Error(err)) => return Err(err),
828839
/// }
840+
/// // Alternately, propagate the error as an io::Error
841+
/// f.try_lock()?;
829842
/// Ok(())
830843
/// }
831844
/// ```
@@ -882,11 +895,14 @@ impl File {
882895
///
883896
/// fn main() -> std::io::Result<()> {
884897
/// let f = File::open("foo.txt")?;
898+
/// // Explicit handling of the WouldBlock error
885899
/// match f.try_lock_shared() {
886900
/// Ok(_) => (),
887901
/// Err(TryLockError::WouldBlock) => (), // Lock not acquired
888902
/// Err(TryLockError::Error(err)) => return Err(err),
889903
/// }
904+
/// // Alternately, propagate the error as an io::Error
905+
/// f.try_lock_shared()?;
890906
///
891907
/// Ok(())
892908
/// }

0 commit comments

Comments
 (0)