|
| 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