|
21 | 21 | mod tests;
|
22 | 22 |
|
23 | 23 | use crate::ffi::OsString;
|
24 |
| -use crate::fmt; |
25 | 24 | use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
|
26 | 25 | use crate::path::{Path, PathBuf};
|
27 | 26 | use crate::sealed::Sealed;
|
28 | 27 | use crate::sync::Arc;
|
29 | 28 | use crate::sys::fs as fs_imp;
|
30 | 29 | use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
|
31 | 30 | use crate::time::SystemTime;
|
| 31 | +use crate::{error, fmt}; |
32 | 32 |
|
33 | 33 | /// An object providing access to an open file on the filesystem.
|
34 | 34 | ///
|
@@ -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,40 @@ 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 | + |
| 389 | +#[unstable(feature = "file_lock", issue = "130994")] |
| 390 | +impl error::Error for TryLockError {} |
| 391 | + |
| 392 | +#[unstable(feature = "file_lock", issue = "130994")] |
| 393 | +impl From<TryLockError> for io::Error { |
| 394 | + fn from(err: TryLockError) -> io::Error { |
| 395 | + match err { |
| 396 | + TryLockError::Error(err) => err, |
| 397 | + TryLockError::WouldBlock => io::ErrorKind::WouldBlock.into(), |
| 398 | + } |
| 399 | + } |
| 400 | +} |
| 401 | + |
355 | 402 | impl File {
|
356 | 403 | /// Attempts to open a file in read-only mode.
|
357 | 404 | ///
|
@@ -734,8 +781,8 @@ impl File {
|
734 | 781 |
|
735 | 782 | /// Try to acquire an exclusive lock on the file.
|
736 | 783 | ///
|
737 |
| - /// Returns `Ok(false)` if a different lock is already held on this file (via another |
738 |
| - /// handle/descriptor). |
| 784 | + /// Returns `Err(TryLockError::WouldBlock)` if a different lock is already held on this file |
| 785 | + /// (via another handle/descriptor). |
739 | 786 | ///
|
740 | 787 | /// This acquires an exclusive lock; no other file handle to this file may acquire another lock.
|
741 | 788 | ///
|
@@ -781,19 +828,19 @@ impl File {
|
781 | 828 | ///
|
782 | 829 | /// fn main() -> std::io::Result<()> {
|
783 | 830 | /// let f = File::create("foo.txt")?;
|
784 |
| - /// f.try_lock()?; |
| 831 | + /// f.try_lock()?; // raises WouldBlock if the lock cannot be acquired |
785 | 832 | /// Ok(())
|
786 | 833 | /// }
|
787 | 834 | /// ```
|
788 | 835 | #[unstable(feature = "file_lock", issue = "130994")]
|
789 |
| - pub fn try_lock(&self) -> io::Result<bool> { |
| 836 | + pub fn try_lock(&self) -> Result<(), TryLockError> { |
790 | 837 | self.inner.try_lock()
|
791 | 838 | }
|
792 | 839 |
|
793 | 840 | /// Try to acquire a shared (non-exclusive) lock on the file.
|
794 | 841 | ///
|
795 |
| - /// Returns `Ok(false)` if an exclusive lock is already held on this file (via another |
796 |
| - /// handle/descriptor). |
| 842 | + /// Returns `Err(TryLockError::WouldBlock)` if a different lock is already held on this file |
| 843 | + /// (via another handle/descriptor). |
797 | 844 | ///
|
798 | 845 | /// This acquires a shared lock; more than one file handle may hold a shared lock, but none may
|
799 | 846 | /// hold an exclusive lock at the same time.
|
@@ -838,12 +885,12 @@ impl File {
|
838 | 885 | ///
|
839 | 886 | /// fn main() -> std::io::Result<()> {
|
840 | 887 | /// let f = File::open("foo.txt")?;
|
841 |
| - /// f.try_lock_shared()?; |
| 888 | + /// f.try_lock_shared()?; // raises WouldBlock if the lock cannot be acquired |
842 | 889 | /// Ok(())
|
843 | 890 | /// }
|
844 | 891 | /// ```
|
845 | 892 | #[unstable(feature = "file_lock", issue = "130994")]
|
846 |
| - pub fn try_lock_shared(&self) -> io::Result<bool> { |
| 893 | + pub fn try_lock_shared(&self) -> Result<(), TryLockError> { |
847 | 894 | self.inner.try_lock_shared()
|
848 | 895 | }
|
849 | 896 |
|
|
0 commit comments