std::sync::Mutex -> tokio::sync::Mutex

This commit is contained in:
newt 2024-11-02 16:29:58 +00:00
parent 2ec4afabcd
commit aad0c697e2
2 changed files with 32 additions and 25 deletions

View file

@ -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"

View file

@ -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<MutexGuard<'_, CookieStore>, PoisonError<MutexGuard<'_, CookieStore>>> {
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<CookieStore> {
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<Item = &HeaderValue>, 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<HeaderValue> {
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<RwLockReadGuard<'_, CookieStore>, PoisonError<RwLockReadGuard<'_, CookieStore>>>
{
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<RwLockWriteGuard<'_, CookieStore>, PoisonError<RwLockWriteGuard<'_, CookieStore>>>
{
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<CookieStore> {
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<Item = &HeaderValue>, 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<HeaderValue> {
let read = self.0.read().unwrap();
let read = block_await!(self.0, read);
cookies(&read, url)
}
}