Skip to main content

wtx/http/
msg_data.rs

1use crate::{
2  collection::Clear,
3  http::Headers,
4  misc::{Lease, LeaseMut, Uri, UriRef},
5};
6use alloc::boxed::Box;
7
8static EMPTY_URI_STRING: UriRef<'static> = UriRef::empty("");
9
10/// An HTTP message data can refer a request or a response.
11///
12/// Groups the elements of an HTTP request/response.
13pub trait MsgData {
14  /// See [`Self::body`].
15  type Body: ?Sized;
16
17  /// Can be a sequence of bytes, a string, a deserialized element or any other desired type.
18  fn body(&self) -> &Self::Body;
19
20  /// See [Headers].
21  fn headers(&self) -> &Headers;
22
23  /// See [`UriRef<'_>`].
24  fn uri(&self) -> UriRef<'_>;
25}
26
27impl<T> MsgData for &T
28where
29  T: MsgData,
30{
31  type Body = T::Body;
32
33  #[inline]
34  fn body(&self) -> &Self::Body {
35    (*self).body()
36  }
37
38  #[inline]
39  fn headers(&self) -> &Headers {
40    (*self).headers()
41  }
42
43  #[inline]
44  fn uri(&self) -> UriRef<'_> {
45    (*self).uri()
46  }
47}
48
49impl<T> MsgData for &mut T
50where
51  T: MsgData,
52{
53  type Body = T::Body;
54
55  #[inline]
56  fn body(&self) -> &Self::Body {
57    (**self).body()
58  }
59
60  #[inline]
61  fn headers(&self) -> &Headers {
62    (**self).headers()
63  }
64
65  #[inline]
66  fn uri(&self) -> UriRef<'_> {
67    (**self).uri()
68  }
69}
70
71impl MsgData for &[u8] {
72  type Body = [u8];
73
74  #[inline]
75  fn body(&self) -> &Self::Body {
76    self
77  }
78
79  #[inline]
80  fn headers(&self) -> &Headers {
81    const { &Headers::new() }
82  }
83
84  #[inline]
85  fn uri(&self) -> UriRef<'_> {
86    EMPTY_URI_STRING
87  }
88}
89
90impl<const N: usize> MsgData for [u8; N] {
91  type Body = [u8; N];
92
93  #[inline]
94  fn body(&self) -> &Self::Body {
95    self
96  }
97
98  #[inline]
99  fn headers(&self) -> &Headers {
100    const { &Headers::new() }
101  }
102
103  #[inline]
104  fn uri(&self) -> UriRef<'_> {
105    EMPTY_URI_STRING
106  }
107}
108
109impl MsgData for () {
110  type Body = [u8];
111
112  #[inline]
113  fn body(&self) -> &Self::Body {
114    &[]
115  }
116
117  #[inline]
118  fn headers(&self) -> &Headers {
119    const { &Headers::new() }
120  }
121
122  #[inline]
123  fn uri(&self) -> UriRef<'_> {
124    EMPTY_URI_STRING
125  }
126}
127
128impl<B, H> MsgData for (B, H)
129where
130  H: Lease<Headers>,
131{
132  type Body = B;
133
134  #[inline]
135  fn body(&self) -> &Self::Body {
136    &self.0
137  }
138
139  #[inline]
140  fn headers(&self) -> &Headers {
141    self.1.lease()
142  }
143
144  #[inline]
145  fn uri(&self) -> UriRef<'_> {
146    EMPTY_URI_STRING
147  }
148}
149
150impl<B, H, S> MsgData for (B, H, Uri<S>)
151where
152  H: Lease<Headers>,
153  S: Lease<str>,
154{
155  type Body = B;
156
157  #[inline]
158  fn body(&self) -> &Self::Body {
159    &self.0
160  }
161
162  #[inline]
163  fn headers(&self) -> &Headers {
164    self.1.lease()
165  }
166
167  #[inline]
168  fn uri(&self) -> UriRef<'_> {
169    self.2.lease().to_ref()
170  }
171}
172
173impl<T> MsgData for Box<T>
174where
175  T: MsgData,
176{
177  type Body = T::Body;
178
179  #[inline]
180  fn body(&self) -> &Self::Body {
181    (**self).body()
182  }
183
184  #[inline]
185  fn headers(&self) -> &Headers {
186    (**self).headers()
187  }
188
189  #[inline]
190  fn uri(&self) -> UriRef<'_> {
191    (**self).uri()
192  }
193}
194
195impl MsgData for Headers {
196  type Body = [u8];
197
198  #[inline]
199  fn body(&self) -> &Self::Body {
200    &[]
201  }
202
203  #[inline]
204  fn headers(&self) -> &Headers {
205    self
206  }
207
208  #[inline]
209  fn uri(&self) -> UriRef<'_> {
210    EMPTY_URI_STRING
211  }
212}
213
214impl<S> MsgData for Uri<S>
215where
216  S: Lease<str>,
217{
218  type Body = [u8];
219
220  #[inline]
221  fn body(&self) -> &Self::Body {
222    &[]
223  }
224
225  #[inline]
226  fn headers(&self) -> &Headers {
227    const { &Headers::new() }
228  }
229
230  #[inline]
231  fn uri(&self) -> UriRef<'_> {
232    self.to_ref()
233  }
234}
235
236/// Mutable version of [`MsgData`].
237pub trait MsgDataMut: MsgData {
238  /// Can be a sequence of mutable bytes, a mutable string or any other desired type.
239  #[inline]
240  fn body_mut(&mut self) -> &mut Self::Body {
241    self.parts_mut().0
242  }
243
244  /// Removes all values.
245  fn clear(&mut self);
246
247  /// Removes all but URI values.
248  fn clear_body_and_headers(&mut self);
249
250  /// Mutable version of [`MsgData::headers`].
251  #[inline]
252  fn headers_mut(&mut self) -> &mut Headers {
253    self.parts_mut().1
254  }
255
256  /// Mutable parts
257  fn parts_mut(&mut self) -> (&mut Self::Body, &mut Headers, UriRef<'_>);
258}
259
260impl<T> MsgDataMut for &mut T
261where
262  T: MsgDataMut,
263{
264  #[inline]
265  fn body_mut(&mut self) -> &mut Self::Body {
266    (**self).body_mut()
267  }
268
269  #[inline]
270  fn clear(&mut self) {
271    (**self).clear();
272  }
273
274  #[inline]
275  fn clear_body_and_headers(&mut self) {
276    (**self).clear_body_and_headers();
277  }
278
279  #[inline]
280  fn headers_mut(&mut self) -> &mut Headers {
281    (**self).headers_mut()
282  }
283
284  #[inline]
285  fn parts_mut(&mut self) -> (&mut Self::Body, &mut Headers, UriRef<'_>) {
286    (**self).parts_mut()
287  }
288}
289
290impl<T> MsgDataMut for Box<T>
291where
292  T: MsgDataMut,
293{
294  #[inline]
295  fn body_mut(&mut self) -> &mut Self::Body {
296    (**self).body_mut()
297  }
298
299  #[inline]
300  fn clear(&mut self) {
301    (**self).clear();
302  }
303
304  #[inline]
305  fn clear_body_and_headers(&mut self) {
306    (**self).clear_body_and_headers();
307  }
308
309  #[inline]
310  fn headers_mut(&mut self) -> &mut Headers {
311    (**self).headers_mut()
312  }
313
314  #[inline]
315  fn parts_mut(&mut self) -> (&mut Self::Body, &mut Headers, UriRef<'_>) {
316    (**self).parts_mut()
317  }
318}
319
320impl MsgDataMut for Headers {
321  #[inline]
322  fn clear(&mut self) {
323    self.headers_mut().clear();
324  }
325
326  #[inline]
327  fn clear_body_and_headers(&mut self) {
328    self.headers_mut().clear();
329  }
330
331  #[inline]
332  fn parts_mut(&mut self) -> (&mut Self::Body, &mut Headers, UriRef<'_>) {
333    (&mut [], self, EMPTY_URI_STRING)
334  }
335}
336
337impl<B, H> MsgDataMut for (B, H)
338where
339  B: Clear,
340  H: LeaseMut<Headers>,
341{
342  #[inline]
343  fn clear(&mut self) {
344    self.body_mut().clear();
345    self.headers_mut().clear();
346  }
347
348  #[inline]
349  fn clear_body_and_headers(&mut self) {
350    self.body_mut().clear();
351    self.headers_mut().clear();
352  }
353
354  #[inline]
355  fn parts_mut(&mut self) -> (&mut Self::Body, &mut Headers, UriRef<'_>) {
356    (&mut self.0, self.1.lease_mut(), EMPTY_URI_STRING)
357  }
358}
359
360impl<B, H, S> MsgDataMut for (B, H, Uri<S>)
361where
362  B: Clear,
363  H: LeaseMut<Headers>,
364  S: Clear + Lease<str>,
365{
366  #[inline]
367  fn clear(&mut self) {
368    self.0.clear();
369    self.1.lease_mut().clear();
370    self.2.clear();
371  }
372
373  #[inline]
374  fn clear_body_and_headers(&mut self) {
375    self.0.clear();
376    self.1.lease_mut().clear();
377  }
378
379  #[inline]
380  fn parts_mut(&mut self) -> (&mut Self::Body, &mut Headers, UriRef<'_>) {
381    (&mut self.0, self.1.lease_mut(), self.2.to_ref())
382  }
383}