11//! HTTP Cookies
22
3- use std:: { convert:: TryInto , fmt, time:: SystemTime } ;
3+ use std:: { convert:: TryInto , fmt, sync :: Arc , time:: SystemTime } ;
44
55use bytes:: BufMut ;
66use 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 ) ]
2539pub struct Cookie < ' a > ( RawCookie < ' a > ) ;
@@ -32,7 +46,37 @@ pub struct Cookie<'a>(RawCookie<'a>);
3246#[ derive( Debug ) ]
3347pub 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+
3680impl < ' 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+
129174impl Jar {
130175 /// Add a cookie str to this jar.
131176 ///
0 commit comments