Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions src/client/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,11 @@ impl ClientBuilder {
/// This requires the optional `cookies` feature to be enabled.
#[inline]
#[cfg(feature = "cookies")]
pub fn cookie_provider<C: cookie::CookieStore + 'static>(
mut self,
cookie_store: Arc<C>,
) -> ClientBuilder {
self.config.cookie_store = Some(cookie_store as _);
pub fn cookie_provider<C>(mut self, cookie_store: C) -> ClientBuilder
where
C: cookie::IntoCookieStore,
{
self.config.cookie_store = Some(cookie_store.into_cookie_store());
self
}

Expand Down Expand Up @@ -1256,13 +1256,13 @@ impl ClientBuilder {

/// Override the DNS resolver implementation.
///
/// Pass an `Arc` wrapping a trait object implementing `Resolve`.
/// Pass an `Arc` wrapping a type implementing `Resolve`.
Comment thread
0x676e67 marked this conversation as resolved.
Outdated
/// Overrides for specific names passed to `resolve` and `resolve_to_addrs` will
/// still be applied on top of this resolver.
#[inline]
pub fn dns_resolver<R>(mut self, resolver: R) -> ClientBuilder
where
R: IntoResolve + Send + Sync + 'static,
R: IntoResolve,
{
self.config.dns_resolver = Some(resolver.into_resolve());
self
Expand Down
47 changes: 46 additions & 1 deletion src/cookie.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! HTTP Cookies

use std::{convert::TryInto, fmt, time::SystemTime};
use std::{convert::TryInto, fmt, sync::Arc, time::SystemTime};

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

/// Trait for converting types into a shared cookie store ([`Arc<dyn CookieStore>`]).
///
/// Implemented for any [`CookieStore`] type, [`Arc<T>`] where `T: CookieStore`, and [`Arc<dyn
/// CookieStore>`]. Enables ergonomic conversion to a trait object for use in APIs without manual
/// boxing.
pub trait IntoCookieStore {
/// Converts the implementor into an [`Arc<dyn CookieStore>`].
///
/// This method allows ergonomic conversion of concrete cookie stores, [`Arc<T>`], or
/// existing [`Arc<dyn CookieStore>`] into a trait object suitable for APIs that expect
/// a shared cookie store.
fn into_cookie_store(self) -> Arc<dyn CookieStore>;
}

/// A single HTTP cookie.
#[derive(Debug, Clone)]
pub struct Cookie<'a>(RawCookie<'a>);
Expand All @@ -32,7 +46,37 @@ pub struct Cookie<'a>(RawCookie<'a>);
#[derive(Debug)]
pub struct Jar(RwLock<cookie_store::CookieStore>);

// ===== impl IntoCookieStore =====

impl IntoCookieStore for Arc<dyn CookieStore> {
#[inline]
fn into_cookie_store(self) -> Arc<dyn CookieStore> {
self
}
}

impl<R> IntoCookieStore for Arc<R>
where
R: CookieStore + 'static,
{
#[inline]
fn into_cookie_store(self) -> Arc<dyn CookieStore> {
self
}
}

impl<R> IntoCookieStore for R
where
R: CookieStore + 'static,
{
#[inline]
fn into_cookie_store(self) -> Arc<dyn CookieStore> {
Arc::new(self)
}
}

// ===== impl Cookie =====

impl<'a> Cookie<'a> {
fn parse(value: &'a HeaderValue) -> crate::Result<Cookie<'a>> {
std::str::from_utf8(value.as_bytes())
Expand Down Expand Up @@ -126,6 +170,7 @@ pub(crate) fn extract_response_cookies(
}

// ===== impl Jar =====

impl Jar {
/// Add a cookie str to this jar.
///
Expand Down
Loading