1
- use crate :: cache:: CacheDirective ;
2
- use crate :: headers:: { HeaderName , HeaderValue , Headers , ToHeaderValues , CACHE_CONTROL } ;
1
+ use headers:: Header ;
2
+
3
+ use crate :: headers:: { HeaderName , HeaderValue , Headers , CACHE_CONTROL } ;
4
+ use crate :: { cache:: CacheDirective , headers} ;
3
5
4
6
use std:: fmt:: { self , Debug , Write } ;
5
7
use std:: iter:: Iterator ;
6
- use std :: option ;
8
+
7
9
use std:: slice;
8
10
9
11
/// A Cache-Control header.
@@ -20,7 +22,7 @@ use std::slice;
20
22
/// entries.push(CacheDirective::NoStore);
21
23
///
22
24
/// let mut res = Response::new(200);
23
- /// entries.apply(&mut res );
25
+ /// res.insert_header(&entries, &entries );
24
26
///
25
27
/// let entries = CacheControl::from_headers(res)?.unwrap();
26
28
/// let mut entries = entries.iter();
@@ -59,31 +61,6 @@ impl CacheControl {
59
61
60
62
Ok ( Some ( Self { entries } ) )
61
63
}
62
-
63
- /// Sets the `Server-Timing` header.
64
- pub fn apply ( & self , mut headers : impl AsMut < Headers > ) {
65
- headers. as_mut ( ) . insert ( CACHE_CONTROL , self . value ( ) ) ;
66
- }
67
-
68
- /// Get the `HeaderName`.
69
- pub fn name ( & self ) -> HeaderName {
70
- CACHE_CONTROL
71
- }
72
-
73
- /// Get the `HeaderValue`.
74
- pub fn value ( & self ) -> HeaderValue {
75
- let mut output = String :: new ( ) ;
76
- for ( n, directive) in self . entries . iter ( ) . enumerate ( ) {
77
- let directive: HeaderValue = directive. clone ( ) . into ( ) ;
78
- match n {
79
- 0 => write ! ( output, "{}" , directive) . unwrap ( ) ,
80
- _ => write ! ( output, ", {}" , directive) . unwrap ( ) ,
81
- } ;
82
- }
83
-
84
- // SAFETY: the internal string is validated to be ASCII.
85
- unsafe { HeaderValue :: from_bytes_unchecked ( output. into ( ) ) }
86
- }
87
64
/// Push a directive into the list of entries.
88
65
pub fn push ( & mut self , directive : CacheDirective ) {
89
66
self . entries . push ( directive) ;
@@ -104,12 +81,22 @@ impl CacheControl {
104
81
}
105
82
}
106
83
107
- impl crate :: headers :: Header for CacheControl {
84
+ impl Header for CacheControl {
108
85
fn header_name ( & self ) -> HeaderName {
109
86
CACHE_CONTROL
110
87
}
111
88
fn header_value ( & self ) -> HeaderValue {
112
- self . value ( )
89
+ let mut output = String :: new ( ) ;
90
+ for ( n, directive) in self . entries . iter ( ) . enumerate ( ) {
91
+ let directive: HeaderValue = directive. clone ( ) . into ( ) ;
92
+ match n {
93
+ 0 => write ! ( output, "{}" , directive) . unwrap ( ) ,
94
+ _ => write ! ( output, ", {}" , directive) . unwrap ( ) ,
95
+ } ;
96
+ }
97
+
98
+ // SAFETY: the internal string is validated to be ASCII.
99
+ unsafe { HeaderValue :: from_bytes_unchecked ( output. into ( ) ) }
113
100
}
114
101
}
115
102
@@ -202,14 +189,6 @@ impl<'a> Iterator for IterMut<'a> {
202
189
}
203
190
}
204
191
205
- impl ToHeaderValues for CacheControl {
206
- type Iter = option:: IntoIter < HeaderValue > ;
207
- fn to_header_values ( & self ) -> crate :: Result < Self :: Iter > {
208
- // A HeaderValue will always convert into itself.
209
- Ok ( self . value ( ) . to_header_values ( ) . unwrap ( ) )
210
- }
211
- }
212
-
213
192
impl Debug for CacheControl {
214
193
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
215
194
let mut list = f. debug_list ( ) ;
0 commit comments