Skip to content
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

Remove the simple-mutex dependency #10

Merged
merged 2 commits into from
Sep 21, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = "async-dup"
version = "1.2.2"
authors = ["Stjepan Glavina <[email protected]>"]
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"
Expand All @@ -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"] }
Expand Down
22 changes: 16 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -226,7 +227,7 @@ where
/// - `impl<T> AsyncWrite for &Mutex<T> where T: AsyncWrite + Unpin {}`
/// - `impl<T> AsyncSeek for Mutex<T> where T: AsyncSeek + Unpin {}`
/// - `impl<T> AsyncSeek for &Mutex<T> where T: AsyncSeek + Unpin {}`
pub struct Mutex<T>(simple_mutex::Mutex<T>);
pub struct Mutex<T>(std::sync::Mutex<T>);

impl<T> Mutex<T> {
/// Creates a new mutex.
Expand Down Expand Up @@ -256,7 +257,7 @@ impl<T> Mutex<T> {
/// 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.
Expand All @@ -278,7 +279,16 @@ impl<T> Mutex<T> {
/// # ;
/// ```
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
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.
Expand All @@ -292,7 +302,7 @@ impl<T> Mutex<T> {
/// 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.
Expand All @@ -310,7 +320,7 @@ impl<T> Mutex<T> {
/// 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())
}
}

Expand Down Expand Up @@ -451,7 +461,7 @@ impl<T: AsyncSeek + Unpin> AsyncSeek for &Mutex<T> {
}

/// 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<T: fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down