Skip to content

Commit 66caf81

Browse files
committed
iterators for ServerTiming events
1 parent b70a849 commit 66caf81

File tree

3 files changed

+141
-22
lines changed

3 files changed

+141
-22
lines changed

src/trace/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
//! - [W3C Trace-Context headers](https://w3c.github.io/trace-context/)
1010
//! - [W3C Server-Timing headers](https://w3c.github.io/server-timing/#the-server-timing-header-field)
1111
12-
mod server_timing;
12+
pub mod server_timing;
1313
mod trace_context;
1414

15-
pub use server_timing::{ServerTiming, TimingEntry};
15+
#[doc(inline)]
16+
pub use server_timing::ServerTiming;
1617
pub use trace_context::TraceContext;

src/trace/server_timing.rs renamed to src/trace/server_timing/entry.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::time::Duration;
22

3-
/// An individual [`ServerTiming`] entry.
3+
/// An individual `ServerTiming` entry.
44
//
55
// # Implementation notes
66
//
@@ -13,14 +13,14 @@ use std::time::Duration;
1313
//
1414
// Multiple different entries per line are supported; separated with a `,`.
1515
#[derive(Debug)]
16-
pub struct TimingEntry {
16+
pub struct Entry {
1717
name: String,
1818
dur: Option<Duration>,
1919
desc: Option<String>,
2020
}
2121

22-
impl TimingEntry {
23-
/// Create a new instance of `TimingEntry`.
22+
impl Entry {
23+
/// Create a new instance of `Entry`.
2424
pub fn new(name: String, dur: Option<Duration>, desc: Option<String>) -> Self {
2525
Self { name, dur, desc }
2626
}
@@ -55,19 +55,3 @@ impl TimingEntry {
5555
self.desc = desc;
5656
}
5757
}
58-
59-
/// Communicate one or more metrics and descriptions for the given request-response cycle.
60-
///
61-
/// This is an implementation of the W3C [Server
62-
/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field)
63-
/// header spec. Read more on
64-
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing).
65-
#[derive(Debug)]
66-
pub struct ServerTiming {
67-
timings: Vec<TimingEntry>,
68-
}
69-
70-
mod test {
71-
const CASE1: &str =
72-
"Server-Timing: metric1; dur=1.1; desc=document, metric1; dur=1.2; desc=document";
73-
}

src/trace/server_timing/mod.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
//! Metrics and descriptions for the given request-response cycle.
2+
3+
mod entry;
4+
5+
pub use entry::Entry;
6+
7+
use std::iter::Iterator;
8+
use std::slice;
9+
10+
/// Metrics and descriptions for the given request-response cycle.
11+
///
12+
/// This is an implementation of the W3C [Server
13+
/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field)
14+
/// header spec. Read more on
15+
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing).
16+
#[derive(Debug)]
17+
pub struct ServerTiming {
18+
timings: Vec<Entry>,
19+
}
20+
21+
impl ServerTiming {
22+
/// An iterator visiting all server timings.
23+
pub fn into_iter(self) -> IntoIter {
24+
IntoIter {
25+
inner: self.timings.into_iter(),
26+
}
27+
}
28+
29+
/// An iterator visiting all server timings.
30+
pub fn iter(&self) -> Iter<'_> {
31+
Iter {
32+
inner: self.timings.iter(),
33+
}
34+
}
35+
36+
/// An iterator visiting all server timings.
37+
pub fn iter_mut(&mut self) -> IterMut<'_> {
38+
IterMut {
39+
inner: self.timings.iter_mut(),
40+
}
41+
}
42+
}
43+
44+
impl IntoIterator for ServerTiming {
45+
type Item = Entry;
46+
type IntoIter = IntoIter;
47+
48+
#[inline]
49+
fn into_iter(self) -> Self::IntoIter {
50+
self.into_iter()
51+
}
52+
}
53+
54+
impl<'a> IntoIterator for &'a ServerTiming {
55+
type Item = &'a Entry;
56+
type IntoIter = Iter<'a>;
57+
58+
#[inline]
59+
fn into_iter(self) -> Self::IntoIter {
60+
self.iter()
61+
}
62+
}
63+
64+
impl<'a> IntoIterator for &'a mut ServerTiming {
65+
type Item = &'a mut Entry;
66+
type IntoIter = IterMut<'a>;
67+
68+
#[inline]
69+
fn into_iter(self) -> Self::IntoIter {
70+
self.iter_mut()
71+
}
72+
}
73+
74+
/// A borrowing iterator over entries in `ServerTiming`.
75+
#[derive(Debug)]
76+
pub struct IntoIter {
77+
inner: std::vec::IntoIter<Entry>,
78+
}
79+
80+
impl Iterator for IntoIter {
81+
type Item = Entry;
82+
83+
fn next(&mut self) -> Option<Self::Item> {
84+
self.inner.next()
85+
}
86+
87+
#[inline]
88+
fn size_hint(&self) -> (usize, Option<usize>) {
89+
self.inner.size_hint()
90+
}
91+
}
92+
93+
/// A lending iterator over entries in `ServerTiming`.
94+
#[derive(Debug)]
95+
pub struct Iter<'a> {
96+
inner: slice::Iter<'a, Entry>,
97+
}
98+
99+
impl<'a> Iterator for Iter<'a> {
100+
type Item = &'a Entry;
101+
102+
fn next(&mut self) -> Option<Self::Item> {
103+
self.inner.next()
104+
}
105+
106+
#[inline]
107+
fn size_hint(&self) -> (usize, Option<usize>) {
108+
self.inner.size_hint()
109+
}
110+
}
111+
112+
/// A mutable iterator over entries in `ServerTiming`.
113+
#[derive(Debug)]
114+
pub struct IterMut<'a> {
115+
inner: slice::IterMut<'a, Entry>,
116+
}
117+
118+
impl<'a> Iterator for IterMut<'a> {
119+
type Item = &'a mut Entry;
120+
121+
fn next(&mut self) -> Option<Self::Item> {
122+
self.inner.next()
123+
}
124+
125+
#[inline]
126+
fn size_hint(&self) -> (usize, Option<usize>) {
127+
self.inner.size_hint()
128+
}
129+
}
130+
131+
mod test {
132+
const CASE1: &str =
133+
"Server-Timing: metric1; dur=1.1; desc=document, metric1; dur=1.2; desc=document";
134+
}

0 commit comments

Comments
 (0)