/rust/registry/src/index.crates.io-6f17d22bba15001f/http-body-1.0.1/src/frame.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use http::HeaderMap; |
2 | | |
3 | | /// A frame of any kind related to an HTTP stream (body). |
4 | | #[derive(Debug)] |
5 | | pub struct Frame<T> { |
6 | | kind: Kind<T>, |
7 | | } |
8 | | |
9 | | #[derive(Debug)] |
10 | | enum Kind<T> { |
11 | | // The first two variants are "inlined" since they are undoubtedly |
12 | | // the most common. This saves us from having to allocate a |
13 | | // boxed trait object for them. |
14 | | Data(T), |
15 | | Trailers(HeaderMap), |
16 | | //Unknown(Box<dyn Frameish>), |
17 | | } |
18 | | |
19 | | impl<T> Frame<T> { |
20 | | /// Create a DATA frame with the provided `Buf`. |
21 | 0 | pub fn data(buf: T) -> Self { |
22 | 0 | Self { |
23 | 0 | kind: Kind::Data(buf), |
24 | 0 | } |
25 | 0 | } |
26 | | |
27 | | /// Create a trailers frame. |
28 | 0 | pub fn trailers(map: HeaderMap) -> Self { |
29 | 0 | Self { |
30 | 0 | kind: Kind::Trailers(map), |
31 | 0 | } |
32 | 0 | } Unexecuted instantiation: <http_body::frame::Frame<bytes::bytes::Bytes>>::trailers Unexecuted instantiation: <http_body::frame::Frame<_>>::trailers |
33 | | |
34 | | /// Maps this frame's data to a different type. |
35 | 0 | pub fn map_data<F, D>(self, f: F) -> Frame<D> |
36 | 0 | where |
37 | 0 | F: FnOnce(T) -> D, |
38 | 0 | { |
39 | 0 | match self.kind { |
40 | 0 | Kind::Data(data) => Frame { |
41 | 0 | kind: Kind::Data(f(data)), |
42 | 0 | }, |
43 | 0 | Kind::Trailers(trailers) => Frame { |
44 | 0 | kind: Kind::Trailers(trailers), |
45 | 0 | }, |
46 | | } |
47 | 0 | } Unexecuted instantiation: <http_body::frame::Frame<bytes::bytes::Bytes>>::map_data::<<bytes::bytes::Bytes as core::convert::Into<bytes::bytes::Bytes>>::into, bytes::bytes::Bytes> Unexecuted instantiation: <http_body::frame::Frame<_>>::map_data::<_, _> |
48 | | |
49 | | /// Returns whether this is a DATA frame. |
50 | 0 | pub fn is_data(&self) -> bool { |
51 | 0 | matches!(self.kind, Kind::Data(..)) |
52 | 0 | } Unexecuted instantiation: <http_body::frame::Frame<bytes::bytes::Bytes>>::is_data Unexecuted instantiation: <http_body::frame::Frame<_>>::is_data |
53 | | |
54 | | /// Consumes self into the buf of the DATA frame. |
55 | | /// |
56 | | /// Returns an [`Err`] containing the original [`Frame`] when frame is not a DATA frame. |
57 | | /// `Frame::is_data` can also be used to determine if the frame is a DATA frame. |
58 | 0 | pub fn into_data(self) -> Result<T, Self> { |
59 | 0 | match self.kind { |
60 | 0 | Kind::Data(data) => Ok(data), |
61 | 0 | _ => Err(self), |
62 | | } |
63 | 0 | } Unexecuted instantiation: <http_body::frame::Frame<bytes::bytes::Bytes>>::into_data Unexecuted instantiation: <http_body::frame::Frame<_>>::into_data |
64 | | |
65 | | /// If this is a DATA frame, returns a reference to it. |
66 | | /// |
67 | | /// Returns `None` if not a DATA frame. |
68 | 0 | pub fn data_ref(&self) -> Option<&T> { |
69 | 0 | match self.kind { |
70 | 0 | Kind::Data(ref data) => Some(data), |
71 | 0 | _ => None, |
72 | | } |
73 | 0 | } Unexecuted instantiation: <http_body::frame::Frame<bytes::bytes::Bytes>>::data_ref Unexecuted instantiation: <http_body::frame::Frame<_>>::data_ref |
74 | | |
75 | | /// If this is a DATA frame, returns a mutable reference to it. |
76 | | /// |
77 | | /// Returns `None` if not a DATA frame. |
78 | 0 | pub fn data_mut(&mut self) -> Option<&mut T> { |
79 | 0 | match self.kind { |
80 | 0 | Kind::Data(ref mut data) => Some(data), |
81 | 0 | _ => None, |
82 | | } |
83 | 0 | } |
84 | | |
85 | | /// Returns whether this is a trailers frame. |
86 | 0 | pub fn is_trailers(&self) -> bool { |
87 | 0 | matches!(self.kind, Kind::Trailers(..)) |
88 | 0 | } Unexecuted instantiation: <http_body::frame::Frame<bytes::bytes::Bytes>>::is_trailers Unexecuted instantiation: <http_body::frame::Frame<_>>::is_trailers |
89 | | |
90 | | /// Consumes self into the buf of the trailers frame. |
91 | | /// |
92 | | /// Returns an [`Err`] containing the original [`Frame`] when frame is not a trailers frame. |
93 | | /// `Frame::is_trailers` can also be used to determine if the frame is a trailers frame. |
94 | 0 | pub fn into_trailers(self) -> Result<HeaderMap, Self> { |
95 | 0 | match self.kind { |
96 | 0 | Kind::Trailers(trailers) => Ok(trailers), |
97 | 0 | _ => Err(self), |
98 | | } |
99 | 0 | } Unexecuted instantiation: <http_body::frame::Frame<bytes::bytes::Bytes>>::into_trailers Unexecuted instantiation: <http_body::frame::Frame<_>>::into_trailers |
100 | | |
101 | | /// If this is a trailers frame, returns a reference to it. |
102 | | /// |
103 | | /// Returns `None` if not a trailers frame. |
104 | 0 | pub fn trailers_ref(&self) -> Option<&HeaderMap> { |
105 | 0 | match self.kind { |
106 | 0 | Kind::Trailers(ref trailers) => Some(trailers), |
107 | 0 | _ => None, |
108 | | } |
109 | 0 | } |
110 | | |
111 | | /// If this is a trailers frame, returns a mutable reference to it. |
112 | | /// |
113 | | /// Returns `None` if not a trailers frame. |
114 | 0 | pub fn trailers_mut(&mut self) -> Option<&mut HeaderMap> { |
115 | 0 | match self.kind { |
116 | 0 | Kind::Trailers(ref mut trailers) => Some(trailers), |
117 | 0 | _ => None, |
118 | | } |
119 | 0 | } |
120 | | } |