Skip to content

Add From<TryLockError> for io::Error #141312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@ impl fmt::Display for TryLockError {
}
}

#[unstable(feature = "file_lock", issue = "130994")]
impl From<TryLockError> for io::Error {
fn from(err: TryLockError) -> io::Error {
match err {
TryLockError::Error(err) => err,
TryLockError::WouldBlock => io::ErrorKind::WouldBlock.into(),
}
}
}

impl File {
/// Attempts to open a file in read-only mode.
///
Expand Down Expand Up @@ -821,11 +831,14 @@ impl File {
///
/// fn main() -> std::io::Result<()> {
/// let f = File::create("foo.txt")?;
/// // Explicit handling of the WouldBlock error
/// match f.try_lock() {
/// Ok(_) => (),
/// Err(TryLockError::WouldBlock) => (), // Lock not acquired
/// Err(TryLockError::Error(err)) => return Err(err),
/// }
/// // Alternately, propagate the error as an io::Error
/// f.try_lock()?;
/// Ok(())
/// }
/// ```
Expand Down Expand Up @@ -882,11 +895,14 @@ impl File {
///
/// fn main() -> std::io::Result<()> {
/// let f = File::open("foo.txt")?;
/// // Explicit handling of the WouldBlock error
/// match f.try_lock_shared() {
/// Ok(_) => (),
/// Err(TryLockError::WouldBlock) => (), // Lock not acquired
/// Err(TryLockError::Error(err)) => return Err(err),
/// }
/// // Alternately, propagate the error as an io::Error
/// f.try_lock_shared()?;
///
/// Ok(())
/// }
Expand Down
22 changes: 22 additions & 0 deletions library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,28 @@ fn file_lock_blocking_async() {
t.join().unwrap();
}

#[test]
#[cfg(windows)]
fn file_try_lock_async() {
const FILE_FLAG_OVERLAPPED: u32 = 0x40000000;

let tmpdir = tmpdir();
let filename = &tmpdir.join("file_try_lock_async.txt");
let f1 = check!(File::create(filename));
let f2 =
check!(OpenOptions::new().custom_flags(FILE_FLAG_OVERLAPPED).write(true).open(filename));

// Check that shared locks block exclusive locks
check!(f1.lock_shared());
assert_matches!(f2.try_lock(), Err(TryLockError::WouldBlock));
check!(f1.unlock());

// Check that exclusive locks block all locks
check!(f1.lock());
assert_matches!(f2.try_lock(), Err(TryLockError::WouldBlock));
assert_matches!(f2.try_lock_shared(), Err(TryLockError::WouldBlock));
}

#[test]
fn file_test_io_seek_shakedown() {
// 01234567890123
Expand Down
10 changes: 2 additions & 8 deletions library/std/src/sys/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,7 @@ impl File {

match result {
Ok(_) => Ok(()),
Err(err)
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
{
Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
Err(TryLockError::WouldBlock)
}
Err(err) => Err(TryLockError::Error(err)),
Expand All @@ -440,10 +437,7 @@ impl File {

match result {
Ok(_) => Ok(()),
Err(err)
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
{
Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
Err(TryLockError::WouldBlock)
}
Err(err) => Err(TryLockError::Error(err)),
Expand Down
Loading