Skip to content

Commit 3382f38

Browse files
committed
feat(cookie): improve cookie_provider for better ergonomics and flexibility
1 parent 938e3f5 commit 3382f38

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

src/client/http/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,11 @@ impl ClientBuilder {
531531
/// This requires the optional `cookies` feature to be enabled.
532532
#[inline]
533533
#[cfg(feature = "cookies")]
534-
pub fn cookie_provider<C: cookie::CookieStore + 'static>(
535-
mut self,
536-
cookie_store: Arc<C>,
537-
) -> ClientBuilder {
538-
self.config.cookie_store = Some(cookie_store as _);
534+
pub fn cookie_provider<C>(mut self, cookie_store: C) -> ClientBuilder
535+
where
536+
C: cookie::IntoCookieStore,
537+
{
538+
self.config.cookie_store = Some(cookie_store.into_cookie_store());
539539
self
540540
}
541541

@@ -1256,13 +1256,13 @@ impl ClientBuilder {
12561256

12571257
/// Override the DNS resolver implementation.
12581258
///
1259-
/// Pass an `Arc` wrapping a trait object implementing `Resolve`.
1259+
/// Pass an `Arc` wrapping a type implementing `Resolve`.
12601260
/// Overrides for specific names passed to `resolve` and `resolve_to_addrs` will
12611261
/// still be applied on top of this resolver.
12621262
#[inline]
12631263
pub fn dns_resolver<R>(mut self, resolver: R) -> ClientBuilder
12641264
where
1265-
R: IntoResolve + Send + Sync + 'static,
1265+
R: IntoResolve,
12661266
{
12671267
self.config.dns_resolver = Some(resolver.into_resolve());
12681268
self

src/cookie.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! HTTP Cookies
22
3-
use std::{convert::TryInto, fmt, time::SystemTime};
3+
use std::{convert::TryInto, fmt, sync::Arc, time::SystemTime};
44

55
use bytes::BufMut;
66
use cookie_crate::{Cookie as RawCookie, Expiration, SameSite};
@@ -20,6 +20,20 @@ pub trait CookieStore: Send + Sync {
2020
fn cookies(&self, url: &url::Url) -> Vec<HeaderValue>;
2121
}
2222

23+
/// Trait for converting types into a shared cookie store ([`Arc<dyn CookieStore>`]).
24+
///
25+
/// Implemented for any [`CookieStore`] type, [`Arc<T>`] where `T: CookieStore`, and [`Arc<dyn
26+
/// CookieStore>`]. Enables ergonomic conversion to a trait object for use in APIs without manual
27+
/// boxing.
28+
pub trait IntoCookieStore {
29+
/// Converts the implementor into an [`Arc<dyn CookieStore>`].
30+
///
31+
/// This method allows ergonomic conversion of concrete cookie stores, [`Arc<T>`], or
32+
/// existing [`Arc<dyn CookieStore>`] into a trait object suitable for APIs that expect
33+
/// a shared cookie store.
34+
fn into_cookie_store(self) -> Arc<dyn CookieStore>;
35+
}
36+
2337
/// A single HTTP cookie.
2438
#[derive(Debug, Clone)]
2539
pub struct Cookie<'a>(RawCookie<'a>);
@@ -32,7 +46,37 @@ pub struct Cookie<'a>(RawCookie<'a>);
3246
#[derive(Debug)]
3347
pub struct Jar(RwLock<cookie_store::CookieStore>);
3448

49+
// ===== impl IntoCookieStore =====
50+
51+
impl IntoCookieStore for Arc<dyn CookieStore> {
52+
#[inline]
53+
fn into_cookie_store(self) -> Arc<dyn CookieStore> {
54+
self
55+
}
56+
}
57+
58+
impl<R> IntoCookieStore for Arc<R>
59+
where
60+
R: CookieStore + 'static,
61+
{
62+
#[inline]
63+
fn into_cookie_store(self) -> Arc<dyn CookieStore> {
64+
self
65+
}
66+
}
67+
68+
impl<R> IntoCookieStore for R
69+
where
70+
R: CookieStore + 'static,
71+
{
72+
#[inline]
73+
fn into_cookie_store(self) -> Arc<dyn CookieStore> {
74+
Arc::new(self)
75+
}
76+
}
77+
3578
// ===== impl Cookie =====
79+
3680
impl<'a> Cookie<'a> {
3781
fn parse(value: &'a HeaderValue) -> crate::Result<Cookie<'a>> {
3882
std::str::from_utf8(value.as_bytes())
@@ -126,6 +170,7 @@ pub(crate) fn extract_response_cookies(
126170
}
127171

128172
// ===== impl Jar =====
173+
129174
impl Jar {
130175
/// Add a cookie str to this jar.
131176
///

0 commit comments

Comments
 (0)