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 ;
@@ -19,9 +40,30 @@ use crate::Headers;
19
40
/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field)
20
41
/// header spec. Read more on
21
42
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing).
43
+ ///
44
+ /// # Examples
45
+ ///
46
+ /// ```
47
+ /// # fn main() -> http_types::Result<()> {
48
+ /// #
49
+ /// use http_types::Response;
50
+ /// use http_types::trace::{ServerTiming, Metric};
51
+ ///
52
+ /// let mut timings = ServerTiming::new();
53
+ /// timings.push(Metric::new("server".to_owned(), None, None)?);
54
+ ///
55
+ /// let mut res = Response::new(200);
56
+ /// timings.apply(&mut res);
57
+ ///
58
+ /// let timings = ServerTiming::from_headers(res)?;
59
+ /// let entry = timings.iter().next().unwrap();
60
+ /// assert_eq!(entry.name(), "server");
61
+ /// #
62
+ /// # Ok(()) }
63
+ /// ```
22
64
#[ derive( Debug ) ]
23
65
pub struct ServerTiming {
24
- timings : Vec < Entry > ,
66
+ timings : Vec < Metric > ,
25
67
}
26
68
27
69
impl ServerTiming {
@@ -48,7 +90,7 @@ impl ServerTiming {
48
90
}
49
91
50
92
/// Push an entry into the list of entries.
51
- pub fn push ( & mut self , entry : Entry ) {
93
+ pub fn push ( & mut self , entry : Metric ) {
52
94
self . timings . push ( entry) ;
53
95
}
54
96
@@ -75,7 +117,7 @@ impl ServerTiming {
75
117
}
76
118
77
119
impl IntoIterator for ServerTiming {
78
- type Item = Entry ;
120
+ type Item = Metric ;
79
121
type IntoIter = IntoIter ;
80
122
81
123
#[ inline]
@@ -85,7 +127,7 @@ impl IntoIterator for ServerTiming {
85
127
}
86
128
87
129
impl < ' a > IntoIterator for & ' a ServerTiming {
88
- type Item = & ' a Entry ;
130
+ type Item = & ' a Metric ;
89
131
type IntoIter = Iter < ' a > ;
90
132
91
133
#[ inline]
@@ -95,7 +137,7 @@ impl<'a> IntoIterator for &'a ServerTiming {
95
137
}
96
138
97
139
impl < ' a > IntoIterator for & ' a mut ServerTiming {
98
- type Item = & ' a mut Entry ;
140
+ type Item = & ' a mut Metric ;
99
141
type IntoIter = IterMut < ' a > ;
100
142
101
143
#[ inline]
@@ -107,11 +149,11 @@ impl<'a> IntoIterator for &'a mut ServerTiming {
107
149
/// A borrowing iterator over entries in `ServerTiming`.
108
150
#[ derive( Debug ) ]
109
151
pub struct IntoIter {
110
- inner : std:: vec:: IntoIter < Entry > ,
152
+ inner : std:: vec:: IntoIter < Metric > ,
111
153
}
112
154
113
155
impl Iterator for IntoIter {
114
- type Item = Entry ;
156
+ type Item = Metric ;
115
157
116
158
fn next ( & mut self ) -> Option < Self :: Item > {
117
159
self . inner . next ( )
@@ -126,11 +168,11 @@ impl Iterator for IntoIter {
126
168
/// A lending iterator over entries in `ServerTiming`.
127
169
#[ derive( Debug ) ]
128
170
pub struct Iter < ' a > {
129
- inner : slice:: Iter < ' a , Entry > ,
171
+ inner : slice:: Iter < ' a , Metric > ,
130
172
}
131
173
132
174
impl < ' a > Iterator for Iter < ' a > {
133
- type Item = & ' a Entry ;
175
+ type Item = & ' a Metric ;
134
176
135
177
fn next ( & mut self ) -> Option < Self :: Item > {
136
178
self . inner . next ( )
@@ -145,11 +187,11 @@ impl<'a> Iterator for Iter<'a> {
145
187
/// A mutable iterator over entries in `ServerTiming`.
146
188
#[ derive( Debug ) ]
147
189
pub struct IterMut < ' a > {
148
- inner : slice:: IterMut < ' a , Entry > ,
190
+ inner : slice:: IterMut < ' a , Metric > ,
149
191
}
150
192
151
193
impl < ' a > Iterator for IterMut < ' a > {
152
- type Item = & ' a mut Entry ;
194
+ type Item = & ' a mut Metric ;
153
195
154
196
fn next ( & mut self ) -> Option < Self :: Item > {
155
197
self . inner . next ( )
@@ -169,7 +211,7 @@ mod test {
169
211
#[ test]
170
212
fn smoke ( ) -> crate :: Result < ( ) > {
171
213
let mut timings = ServerTiming :: new ( ) ;
172
- timings. push ( Entry :: new ( "server" . to_owned ( ) , None , None ) ?) ;
214
+ timings. push ( Metric :: new ( "server" . to_owned ( ) , None , None ) ?) ;
173
215
174
216
let mut headers = Headers :: new ( ) ;
175
217
timings. apply ( & mut headers) ;
0 commit comments