Skip to content

Commit 2caf278

Browse files
committed
Change File::try_lock() and try_lock_shared() to return io::Result<()>
1 parent 1a95cc6 commit 2caf278

File tree

9 files changed

+52
-135
lines changed

9 files changed

+52
-135
lines changed

library/std/src/fs.rs

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
mod tests;
2222

2323
use crate::ffi::OsString;
24+
use crate::fmt;
2425
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
2526
use crate::path::{Path, PathBuf};
2627
use crate::sealed::Sealed;
2728
use crate::sync::Arc;
2829
use crate::sys::fs as fs_imp;
2930
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
3031
use crate::time::SystemTime;
31-
use crate::{error, fmt};
3232

3333
/// An object providing access to an open file on the filesystem.
3434
///
@@ -116,22 +116,6 @@ 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. 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-
135119
/// Metadata information about a file.
136120
///
137121
/// This structure is returned from the [`metadata`] or
@@ -368,30 +352,6 @@ pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result
368352
inner(path.as_ref(), contents.as_ref())
369353
}
370354

371-
#[unstable(feature = "file_lock", issue = "130994")]
372-
impl error::Error for TryLockError {}
373-
374-
#[unstable(feature = "file_lock", issue = "130994")]
375-
impl fmt::Debug for TryLockError {
376-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
377-
match self {
378-
TryLockError::Error(err) => err.fmt(f),
379-
TryLockError::WouldBlock => "WouldBlock".fmt(f),
380-
}
381-
}
382-
}
383-
384-
#[unstable(feature = "file_lock", issue = "130994")]
385-
impl fmt::Display for TryLockError {
386-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
387-
match self {
388-
TryLockError::Error(_) => "lock acquisition failed due to I/O error",
389-
TryLockError::WouldBlock => "lock acquisition failed because the operation would block",
390-
}
391-
.fmt(f)
392-
}
393-
}
394-
395355
impl File {
396356
/// Attempts to open a file in read-only mode.
397357
///
@@ -774,7 +734,7 @@ impl File {
774734

775735
/// Try to acquire an exclusive lock on the file.
776736
///
777-
/// Returns `Err(TryLockError::WouldBlock)` if a different lock is already held on this file
737+
/// Returns an error with kind [`WouldBlock`] if a different lock is already held on this file
778738
/// (via another handle/descriptor).
779739
///
780740
/// This acquires an exclusive lock; no other file handle to this file may acquire another lock.
@@ -812,31 +772,28 @@ impl File {
812772
/// [`unlock`]: File::unlock
813773
/// [`read`]: Read::read
814774
/// [`write`]: Write::write
775+
/// [`WouldBlock`]: io::ErrorKind::WouldBlock
815776
///
816777
/// # Examples
817778
///
818779
/// ```no_run
819780
/// #![feature(file_lock)]
820-
/// use std::fs::{File, TryLockError};
781+
/// use std::fs::File;
821782
///
822783
/// fn main() -> std::io::Result<()> {
823784
/// let f = File::create("foo.txt")?;
824-
/// match f.try_lock() {
825-
/// Ok(_) => (),
826-
/// Err(TryLockError::WouldBlock) => (), // Lock not acquired
827-
/// Err(TryLockError::Error(err)) => return Err(err),
828-
/// }
785+
/// f.try_lock()?;
829786
/// Ok(())
830787
/// }
831788
/// ```
832789
#[unstable(feature = "file_lock", issue = "130994")]
833-
pub fn try_lock(&self) -> Result<(), TryLockError> {
790+
pub fn try_lock(&self) -> io::Result<()> {
834791
self.inner.try_lock()
835792
}
836793

837794
/// Try to acquire a shared (non-exclusive) lock on the file.
838795
///
839-
/// Returns `Err(TryLockError::WouldBlock)` if a different lock is already held on this file
796+
/// Returns an error with kind [`WouldBlock`] if a different lock is already held on this file
840797
/// (via another handle/descriptor).
841798
///
842799
/// This acquires a shared lock; more than one file handle may hold a shared lock, but none may
@@ -873,26 +830,23 @@ impl File {
873830
/// [`unlock`]: File::unlock
874831
/// [`read`]: Read::read
875832
/// [`write`]: Write::write
833+
/// [`WouldBlock`]: io::ErrorKind::WouldBlock
876834
///
877835
/// # Examples
878836
///
879837
/// ```no_run
880838
/// #![feature(file_lock)]
881-
/// use std::fs::{File, TryLockError};
839+
/// use std::fs::File;
882840
///
883841
/// fn main() -> std::io::Result<()> {
884842
/// let f = File::open("foo.txt")?;
885-
/// match f.try_lock_shared() {
886-
/// Ok(_) => (),
887-
/// Err(TryLockError::WouldBlock) => (), // Lock not acquired
888-
/// Err(TryLockError::Error(err)) => return Err(err),
889-
/// }
843+
/// f.try_lock_shared()?;
890844
///
891845
/// Ok(())
892846
/// }
893847
/// ```
894848
#[unstable(feature = "file_lock", issue = "130994")]
895-
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
849+
pub fn try_lock_shared(&self) -> io::Result<()> {
896850
self.inner.try_lock_shared()
897851
}
898852

library/std/src/fs/tests.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ use rand::RngCore;
99
))]
1010
use crate::assert_matches::assert_matches;
1111
use crate::char::MAX_LEN_UTF8;
12-
#[cfg(any(
13-
windows,
14-
target_os = "freebsd",
15-
target_os = "linux",
16-
target_os = "netbsd",
17-
target_vendor = "apple",
18-
))]
19-
use crate::fs::TryLockError;
2012
use crate::fs::{self, File, FileTimes, OpenOptions};
2113
use crate::io::prelude::*;
2214
use crate::io::{BorrowedBuf, ErrorKind, SeekFrom};
@@ -259,12 +251,12 @@ fn file_lock_blocking() {
259251

260252
// Check that shared locks block exclusive locks
261253
check!(f1.lock_shared());
262-
assert_matches!(f2.try_lock(), Err(TryLockError::WouldBlock));
254+
assert_matches!(f2.try_lock().unwrap_err().kind(), ErrorKind::WouldBlock);
263255
check!(f1.unlock());
264256

265257
// Check that exclusive locks block shared locks
266258
check!(f1.lock());
267-
assert_matches!(f2.try_lock_shared(), Err(TryLockError::WouldBlock));
259+
assert_matches!(f2.try_lock_shared().unwrap_err().kind(), ErrorKind::WouldBlock);
268260
}
269261

270262
#[test]
@@ -283,7 +275,7 @@ fn file_lock_drop() {
283275

284276
// Check that locks are released when the File is dropped
285277
check!(f1.lock_shared());
286-
assert_matches!(f2.try_lock(), Err(TryLockError::WouldBlock));
278+
assert_matches!(f2.try_lock().unwrap_err().kind(), ErrorKind::WouldBlock);
287279
drop(f1);
288280
check!(f2.try_lock());
289281
}
@@ -304,10 +296,10 @@ fn file_lock_dup() {
304296

305297
// Check that locks are not dropped if the File has been cloned
306298
check!(f1.lock_shared());
307-
assert_matches!(f2.try_lock(), Err(TryLockError::WouldBlock));
299+
assert_matches!(f2.try_lock().unwrap_err().kind(), ErrorKind::WouldBlock);
308300
let cloned = check!(f1.try_clone());
309301
drop(f1);
310-
assert_matches!(f2.try_lock(), Err(TryLockError::WouldBlock));
302+
assert_matches!(f2.try_lock().unwrap_err().kind(), ErrorKind::WouldBlock);
311303
drop(cloned)
312304
}
313305

@@ -323,7 +315,7 @@ fn file_lock_double_unlock() {
323315
// Check that both are released by unlock()
324316
check!(f1.lock());
325317
check!(f1.lock_shared());
326-
assert_matches!(f2.try_lock(), Err(TryLockError::WouldBlock));
318+
assert_matches!(f2.try_lock().unwrap_err().kind(), ErrorKind::WouldBlock);
327319
check!(f1.unlock());
328320
check!(f2.try_lock());
329321
}

library/std/src/sys/fs/hermit.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::ffi::{CStr, OsStr, OsString, c_char};
2-
use crate::fs::TryLockError;
32
use crate::io::{self, BorrowedCursor, Error, ErrorKind, IoSlice, IoSliceMut, SeekFrom};
43
use crate::os::hermit::ffi::OsStringExt;
54
use crate::os::hermit::hermit_abi::{
@@ -13,7 +12,7 @@ use crate::sys::common::small_c_string::run_path_with_cstr;
1312
use crate::sys::fd::FileDesc;
1413
pub use crate::sys::fs::common::{copy, exists};
1514
use crate::sys::time::SystemTime;
16-
use crate::sys::{cvt, unsupported, unsupported_err};
15+
use crate::sys::{cvt, unsupported};
1716
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
1817
use crate::{fmt, mem};
1918

@@ -367,12 +366,12 @@ impl File {
367366
unsupported()
368367
}
369368

370-
pub fn try_lock(&self) -> Result<(), TryLockError> {
371-
Err(TryLockError::Error(unsupported_err()))
369+
pub fn try_lock(&self) -> io::Result<()> {
370+
unsupported()
372371
}
373372

374-
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
375-
Err(TryLockError::Error(unsupported_err()))
373+
pub fn try_lock_shared(&self) -> io::Result<()> {
374+
unsupported()
376375
}
377376

378377
pub fn unlock(&self) -> io::Result<()> {

library/std/src/sys/fs/solid.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use crate::ffi::{CStr, CString, OsStr, OsString};
44
use crate::fmt;
5-
use crate::fs::TryLockError;
65
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
76
use crate::mem::MaybeUninit;
87
use crate::os::raw::{c_int, c_short};
@@ -12,7 +11,7 @@ use crate::sync::Arc;
1211
pub use crate::sys::fs::common::exists;
1312
use crate::sys::pal::{abi, error};
1413
use crate::sys::time::SystemTime;
15-
use crate::sys::{unsupported, unsupported_err};
14+
use crate::sys::unsupported;
1615
use crate::sys_common::ignore_notfound;
1716

1817
type CIntNotMinusOne = core::num::niche_types::NotAllOnes<c_int>;
@@ -353,12 +352,12 @@ impl File {
353352
unsupported()
354353
}
355354

356-
pub fn try_lock(&self) -> Result<(), TryLockError> {
357-
Err(TryLockError::Error(unsupported_err()))
355+
pub fn try_lock(&self) -> io::Result<()> {
356+
unsupported()
358357
}
359358

360-
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
361-
Err(TryLockError::Error(unsupported_err()))
359+
pub fn try_lock_shared(&self) -> io::Result<()> {
360+
unsupported()
362361
}
363362

364363
pub fn unlock(&self) -> io::Result<()> {

library/std/src/sys/fs/uefi.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use r_efi::protocols::file;
22

33
use crate::ffi::OsString;
44
use crate::fmt;
5-
use crate::fs::TryLockError;
65
use crate::hash::Hash;
76
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
87
use crate::path::{Path, PathBuf};
@@ -228,11 +227,11 @@ impl File {
228227
self.0
229228
}
230229

231-
pub fn try_lock(&self) -> Result<(), TryLockError> {
230+
pub fn try_lock(&self) -> io::Result<()> {
232231
self.0
233232
}
234233

235-
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
234+
pub fn try_lock_shared(&self) -> io::Result<()> {
236235
self.0
237236
}
238237

library/std/src/sys/fs/unix.rs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ use libc::{dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, st
7575

7676
use crate::ffi::{CStr, OsStr, OsString};
7777
use crate::fmt::{self, Write as _};
78-
use crate::fs::TryLockError;
7978
use crate::io::{self, BorrowedCursor, Error, IoSlice, IoSliceMut, SeekFrom};
8079
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd};
8180
use crate::os::unix::prelude::*;
@@ -1311,17 +1310,9 @@ impl File {
13111310
target_os = "netbsd",
13121311
target_vendor = "apple",
13131312
))]
1314-
pub fn try_lock(&self) -> Result<(), TryLockError> {
1315-
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) });
1316-
if let Err(err) = result {
1317-
if err.kind() == io::ErrorKind::WouldBlock {
1318-
Err(TryLockError::WouldBlock)
1319-
} else {
1320-
Err(TryLockError::Error(err))
1321-
}
1322-
} else {
1323-
Ok(())
1324-
}
1313+
pub fn try_lock(&self) -> io::Result<()> {
1314+
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) })?;
1315+
return Ok(());
13251316
}
13261317

13271318
#[cfg(not(any(
@@ -1331,11 +1322,8 @@ impl File {
13311322
target_os = "netbsd",
13321323
target_vendor = "apple",
13331324
)))]
1334-
pub fn try_lock(&self) -> Result<(), TryLockError> {
1335-
Err(TryLockError::Error(io::const_error!(
1336-
io::ErrorKind::Unsupported,
1337-
"try_lock() not supported"
1338-
)))
1325+
pub fn try_lock(&self) -> io::Result<()> {
1326+
Err(io::const_error!(io::ErrorKind::Unsupported, "try_lock() not supported"))
13391327
}
13401328

13411329
#[cfg(any(
@@ -1345,17 +1333,9 @@ impl File {
13451333
target_os = "netbsd",
13461334
target_vendor = "apple",
13471335
))]
1348-
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
1349-
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) });
1350-
if let Err(err) = result {
1351-
if err.kind() == io::ErrorKind::WouldBlock {
1352-
Err(TryLockError::WouldBlock)
1353-
} else {
1354-
Err(TryLockError::Error(err))
1355-
}
1356-
} else {
1357-
Ok(())
1358-
}
1336+
pub fn try_lock_shared(&self) -> io::Result<()> {
1337+
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) })?;
1338+
return Ok(());
13591339
}
13601340

13611341
#[cfg(not(any(
@@ -1365,11 +1345,8 @@ impl File {
13651345
target_os = "netbsd",
13661346
target_vendor = "apple",
13671347
)))]
1368-
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
1369-
Err(TryLockError::Error(io::const_error!(
1370-
io::ErrorKind::Unsupported,
1371-
"try_lock_shared() not supported"
1372-
)))
1348+
pub fn try_lock_shared(&self) -> io::Result<()> {
1349+
Err(io::const_error!(io::ErrorKind::Unsupported, "try_lock_shared() not supported"))
13731350
}
13741351

13751352
#[cfg(any(

library/std/src/sys/fs/unsupported.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::ffi::OsString;
22
use crate::fmt;
3-
use crate::fs::TryLockError;
43
use crate::hash::{Hash, Hasher};
54
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
65
use crate::path::{Path, PathBuf};
@@ -207,11 +206,11 @@ impl File {
207206
self.0
208207
}
209208

210-
pub fn try_lock(&self) -> Result<(), TryLockError> {
209+
pub fn try_lock(&self) -> io::Result<()> {
211210
self.0
212211
}
213212

214-
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
213+
pub fn try_lock_shared(&self) -> io::Result<()> {
215214
self.0
216215
}
217216

0 commit comments

Comments
 (0)