From 463d008446f7a868df05aaea174791bd5ed1473d Mon Sep 17 00:00:00 2001 From: John Nunley Date: Wed, 20 Sep 2023 19:22:40 -0700 Subject: [PATCH 1/2] Remove the simple-mutex dependency It appears that this was originally done when std::sync::Mutex used the PThreads implementation, which was much slower than other contemporary Mutex implementations (like parking_lot). Therefore it made sense to use a custom Mutex implementation here. However std Mutexes now use futexes, which are much faster than the previous implementation. In addition, the original git history for simple-mutex appears to be lost to time. The only copy of the source code is on crates.io, and the crate is not owned by anyone. So it is problematic to use it anyways, as updating it would require us to go through the painstaking process of reclaiming the name. This commit removes the dependency on simple-mutex and replaces its usages with std::sync::Mutex for the reasons listed above. The performance impact of this change has not been measured, but I believe it to be negligible. Signed-off-by: John Nunley --- Cargo.toml | 1 - src/lib.rs | 22 ++++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 273a19b..e1d5494 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,6 @@ exclude = ["/.*"] [dependencies] futures-io = "0.3.5" -simple-mutex = "1.1.5" [dev-dependencies] futures = { version = "0.3.5", default-features = false, features = ["std"] } diff --git a/src/lib.rs b/src/lib.rs index 446dab1..2b5db2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,6 +60,7 @@ use std::hash::{Hash, Hasher}; use std::io::{self, IoSlice, IoSliceMut, SeekFrom}; use std::ops::{Deref, DerefMut}; use std::pin::Pin; +use std::sync::TryLockError; use std::task::{Context, Poll}; use futures_io::{AsyncRead, AsyncSeek, AsyncWrite}; @@ -226,7 +227,7 @@ where /// - `impl AsyncWrite for &Mutex where T: AsyncWrite + Unpin {}` /// - `impl AsyncSeek for Mutex where T: AsyncSeek + Unpin {}` /// - `impl AsyncSeek for &Mutex where T: AsyncSeek + Unpin {}` -pub struct Mutex(simple_mutex::Mutex); +pub struct Mutex(std::sync::Mutex); impl Mutex { /// Creates a new mutex. @@ -256,7 +257,7 @@ impl Mutex { /// assert_eq!(*guard, 10); /// ``` pub fn lock(&self) -> MutexGuard<'_, T> { - MutexGuard(self.0.lock()) + MutexGuard(self.0.lock().unwrap_or_else(|e| e.into_inner())) } /// Attempts to acquire the mutex. @@ -278,7 +279,16 @@ impl Mutex { /// # ; /// ``` pub fn try_lock(&self) -> Option> { - self.0.try_lock().map(MutexGuard) + self.0 + .try_lock() + .map_or_else( + |e| match e { + TryLockError::Poisoned(e) => Some(e.into_inner()), + TryLockError::WouldBlock => None, + }, + Some, + ) + .map(MutexGuard) } /// Consumes the mutex, returning the underlying data. @@ -292,7 +302,7 @@ impl Mutex { /// assert_eq!(mutex.into_inner(), 10); /// ``` pub fn into_inner(self) -> T { - self.0.into_inner() + self.0.into_inner().unwrap_or_else(|e| e.into_inner()) } /// Returns a mutable reference to the underlying data. @@ -310,7 +320,7 @@ impl Mutex { /// assert_eq!(*mutex.lock(), 10); /// ``` pub fn get_mut(&mut self) -> &mut T { - self.0.get_mut() + self.0.get_mut().unwrap_or_else(|e| e.into_inner()) } } @@ -451,7 +461,7 @@ impl AsyncSeek for &Mutex { } /// A guard that releases the mutex when dropped. -pub struct MutexGuard<'a, T>(simple_mutex::MutexGuard<'a, T>); +pub struct MutexGuard<'a, T>(std::sync::MutexGuard<'a, T>); impl fmt::Debug for MutexGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { From 5480620672b9e901a755de9d08ce6c3936df12bc Mon Sep 17 00:00:00 2001 From: John Nunley Date: Wed, 20 Sep 2023 19:26:07 -0700 Subject: [PATCH 2/2] Bump MSRV to 1.41 Signed-off-by: John Nunley --- .github/workflows/ci.yml | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2c77be3..6082200 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,7 +49,7 @@ jobs: matrix: # When updating this, the reminder to update the minimum supported # Rust version in Cargo.toml. - rust: ['1.36'] + rust: ['1.41.0'] steps: - uses: actions/checkout@v4 - name: Install Rust diff --git a/Cargo.toml b/Cargo.toml index e1d5494..a7f8f36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ name = "async-dup" version = "1.2.2" authors = ["Stjepan Glavina "] edition = "2018" -rust-version = "1.36" +rust-version = "1.41" description = "Duplicate an async I/O handle" license = "Apache-2.0 OR MIT" repository = "https://github.com/smol-rs/async-dup"