From aad0c697e209c6e79b15ff49b3bf0d05663492c3 Mon Sep 17 00:00:00 2001 From: newt Date: Sat, 2 Nov 2024 16:29:58 +0000 Subject: [PATCH] std::sync::Mutex -> tokio::sync::Mutex --- Cargo.toml | 2 ++ src/lib.rs | 55 +++++++++++++++++++++++++++++------------------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 111be4b..79e28e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,8 @@ reqwest = { version = "^0.12", default-features = false, features = ["cookies"] url = "2.2.2" serde = {version = "1.0.147", optional = true} serde_derive = {version = "1.0.147" , optional = true } +tokio = { version = "1.41.0", default-features = false } +futures = { version = "0.3.31", default-features = false, features = ["executor"] } [dev-dependencies] tokio-test = "0.4.1" diff --git a/src/lib.rs b/src/lib.rs index 6b0fae5..b111775 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,9 +79,10 @@ //! # }); //!``` -use std::{ - ops::Deref, - sync::{LockResult, Mutex, MutexGuard, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard}, +use std::ops::Deref; +use tokio::{ + runtime::Handle, + sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}, }; use bytes::Bytes; @@ -135,31 +136,41 @@ impl Default for CookieStoreMutex { impl CookieStoreMutex { /// Create a new [`CookieStoreMutex`] from an existing [`cookie_store::CookieStore`]. - pub const fn new(cookie_store: CookieStore) -> CookieStoreMutex { + pub fn new(cookie_store: CookieStore) -> CookieStoreMutex { CookieStoreMutex(Mutex::new(cookie_store)) } /// Lock and get a handle to the contained [`cookie_store::CookieStore`]. - pub fn lock( - &self, - ) -> Result, PoisonError>> { - self.0.lock() + pub async fn lock(&self) -> MutexGuard<'_, CookieStore> { + self.0.lock().await } /// Consumes this [`CookieStoreMutex`], returning the underlying [`cookie_store::CookieStore`] - pub fn into_inner(self) -> LockResult { + pub fn into_inner(self) -> CookieStore { self.0.into_inner() } } +macro_rules! block_await { + ($obj:expr, $func:ident) => { + futures::executor::block_on({ + { + let handle = Handle::current(); + let _ = handle.enter(); + $obj.$func() + } + }) + }; +} + impl reqwest::cookie::CookieStore for CookieStoreMutex { fn set_cookies(&self, cookie_headers: &mut dyn Iterator, url: &url::Url) { - let mut store = self.0.lock().unwrap(); + let mut store = block_await!(self.0, lock); set_cookies(&mut store, cookie_headers, url); } fn cookies(&self, url: &url::Url) -> Option { - let store = self.0.lock().unwrap(); + let store = block_await!(self.0, lock); cookies(&store, url) } } @@ -179,40 +190,34 @@ impl Default for CookieStoreRwLock { impl CookieStoreRwLock { /// Create a new [`CookieStoreRwLock`] from an existing [`cookie_store::CookieStore`]. - pub const fn new(cookie_store: CookieStore) -> CookieStoreRwLock { + pub fn new(cookie_store: CookieStore) -> CookieStoreRwLock { CookieStoreRwLock(RwLock::new(cookie_store)) } /// Lock and get a read (non-exclusive) handle to the contained [`cookie_store::CookieStore`]. - pub fn read( - &self, - ) -> Result, PoisonError>> - { - self.0.read() + pub async fn read(&self) -> RwLockReadGuard<'_, CookieStore> { + self.0.read().await } /// Lock and get a write (exclusive) handle to the contained [`cookie_store::CookieStore`]. - pub fn write( - &self, - ) -> Result, PoisonError>> - { - self.0.write() + pub async fn write(&self) -> RwLockWriteGuard<'_, CookieStore> { + self.0.write().await } /// Consume this [`CookieStoreRwLock`], returning the underlying [`cookie_store::CookieStore`] - pub fn into_inner(self) -> LockResult { + pub fn into_inner(self) -> CookieStore { self.0.into_inner() } } impl reqwest::cookie::CookieStore for CookieStoreRwLock { fn set_cookies(&self, cookie_headers: &mut dyn Iterator, url: &url::Url) { - let mut write = self.0.write().unwrap(); + let mut write = block_await!(self.0, write); set_cookies(&mut write, cookie_headers, url); } fn cookies(&self, url: &url::Url) -> Option { - let read = self.0.read().unwrap(); + let read = block_await!(self.0, read); cookies(&read, url) } }