1
1
//! Metrics and descriptions for the given request-response cycle.
2
-
3
- mod entry;
2
+ //!
3
+ //! # Examples
4
+ //!
5
+ //! ```
6
+ //! # fn main() -> http_types::Result<()> {
7
+ //! #
8
+ //! use http_types::Response;
9
+ //! use http_types::trace::{ServerTiming, Metric};
10
+ //!
11
+ //! let mut timings = ServerTiming::new();
12
+ //! timings.push(Metric::new("server".to_owned(), None, None)?);
13
+ //!
14
+ //! let mut res = Response::new(200);
15
+ //! timings.apply(&mut res);
16
+ //!
17
+ //! let timings = ServerTiming::from_headers(res)?;
18
+ //! let entry = timings.iter().next().unwrap();
19
+ //! assert_eq!(entry.name(), "server");
20
+ //! #
21
+ //! # Ok(()) }
22
+ //! ```
23
+
24
+ mod metric;
4
25
mod parse;
5
26
6
- pub use entry :: Entry ;
27
+ pub use metric :: Metric ;
7
28
use parse:: parse_header;
8
29
9
30
use std:: convert:: AsMut ;
@@ -20,9 +41,30 @@ use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, SERVER_TI
20
41
/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field)
21
42
/// header spec. Read more on
22
43
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing).
44
+ ///
45
+ /// # Examples
46
+ ///
47
+ /// ```
48
+ /// # fn main() -> http_types::Result<()> {
49
+ /// #
50
+ /// use http_types::Response;
51
+ /// use http_types::trace::{ServerTiming, Metric};
52
+ ///
53
+ /// let mut timings = ServerTiming::new();
54
+ /// timings.push(Metric::new("server".to_owned(), None, None)?);
55
+ ///
56
+ /// let mut res = Response::new(200);
57
+ /// timings.apply(&mut res);
58
+ ///
59
+ /// let timings = ServerTiming::from_headers(res)?;
60
+ /// let entry = timings.iter().next().unwrap();
61
+ /// assert_eq!(entry.name(), "server");
62
+ /// #
63
+ /// # Ok(()) }
64
+ /// ```
23
65
#[ derive( Debug ) ]
24
66
pub struct ServerTiming {
25
- timings : Vec < Entry > ,
67
+ timings : Vec < Metric > ,
26
68
}
27
69
28
70
impl ServerTiming {
@@ -68,7 +110,7 @@ impl ServerTiming {
68
110
}
69
111
70
112
/// Push an entry into the list of entries.
71
- pub fn push ( & mut self , entry : Entry ) {
113
+ pub fn push ( & mut self , entry : Metric ) {
72
114
self . timings . push ( entry) ;
73
115
}
74
116
@@ -95,7 +137,7 @@ impl ServerTiming {
95
137
}
96
138
97
139
impl IntoIterator for ServerTiming {
98
- type Item = Entry ;
140
+ type Item = Metric ;
99
141
type IntoIter = IntoIter ;
100
142
101
143
#[ inline]
@@ -105,7 +147,7 @@ impl IntoIterator for ServerTiming {
105
147
}
106
148
107
149
impl < ' a > IntoIterator for & ' a ServerTiming {
108
- type Item = & ' a Entry ;
150
+ type Item = & ' a Metric ;
109
151
type IntoIter = Iter < ' a > ;
110
152
111
153
// #[inline]serv
@@ -115,7 +157,7 @@ impl<'a> IntoIterator for &'a ServerTiming {
115
157
}
116
158
117
159
impl < ' a > IntoIterator for & ' a mut ServerTiming {
118
- type Item = & ' a mut Entry ;
160
+ type Item = & ' a mut Metric ;
119
161
type IntoIter = IterMut < ' a > ;
120
162
121
163
#[ inline]
@@ -127,11 +169,11 @@ impl<'a> IntoIterator for &'a mut ServerTiming {
127
169
/// A borrowing iterator over entries in `ServerTiming`.
128
170
#[ derive( Debug ) ]
129
171
pub struct IntoIter {
130
- inner : std:: vec:: IntoIter < Entry > ,
172
+ inner : std:: vec:: IntoIter < Metric > ,
131
173
}
132
174
133
175
impl Iterator for IntoIter {
134
- type Item = Entry ;
176
+ type Item = Metric ;
135
177
136
178
fn next ( & mut self ) -> Option < Self :: Item > {
137
179
self . inner . next ( )
@@ -146,11 +188,11 @@ impl Iterator for IntoIter {
146
188
/// A lending iterator over entries in `ServerTiming`.
147
189
#[ derive( Debug ) ]
148
190
pub struct Iter < ' a > {
149
- inner : slice:: Iter < ' a , Entry > ,
191
+ inner : slice:: Iter < ' a , Metric > ,
150
192
}
151
193
152
194
impl < ' a > Iterator for Iter < ' a > {
153
- type Item = & ' a Entry ;
195
+ type Item = & ' a Metric ;
154
196
155
197
fn next ( & mut self ) -> Option < Self :: Item > {
156
198
self . inner . next ( )
@@ -165,11 +207,11 @@ impl<'a> Iterator for Iter<'a> {
165
207
/// A mutable iterator over entries in `ServerTiming`.
166
208
#[ derive( Debug ) ]
167
209
pub struct IterMut < ' a > {
168
- inner : slice:: IterMut < ' a , Entry > ,
210
+ inner : slice:: IterMut < ' a , Metric > ,
169
211
}
170
212
171
213
impl < ' a > Iterator for IterMut < ' a > {
172
- type Item = & ' a mut Entry ;
214
+ type Item = & ' a mut Metric ;
173
215
174
216
fn next ( & mut self ) -> Option < Self :: Item > {
175
217
self . inner . next ( )
@@ -197,7 +239,7 @@ mod test {
197
239
#[ test]
198
240
fn smoke ( ) -> crate :: Result < ( ) > {
199
241
let mut timings = ServerTiming :: new ( ) ;
200
- timings. push ( Entry :: new ( "server" . to_owned ( ) , None , None ) ?) ;
242
+ timings. push ( Metric :: new ( "server" . to_owned ( ) , None , None ) ?) ;
201
243
202
244
let mut headers = Headers :: new ( ) ;
203
245
timings. apply ( & mut headers) ;
0 commit comments