pavex/response/body/
bytes.rs

1use std::borrow::Cow;
2
3use bytes::{Bytes, BytesMut};
4use http_body_util::Full;
5
6use crate::http::HeaderValue;
7
8use super::TypedBody;
9
10impl TypedBody for Bytes {
11    type Body = Full<Bytes>;
12
13    fn content_type(&self) -> HeaderValue {
14        HeaderValue::from_static(mime::APPLICATION_OCTET_STREAM.as_ref())
15    }
16
17    fn body(self) -> Self::Body {
18        Full::new(self)
19    }
20}
21
22impl TypedBody for Vec<u8> {
23    type Body = Full<Bytes>;
24
25    fn content_type(&self) -> HeaderValue {
26        HeaderValue::from_static(mime::APPLICATION_OCTET_STREAM.as_ref())
27    }
28
29    fn body(self) -> Self::Body {
30        Full::new(self.into())
31    }
32}
33
34impl TypedBody for &'static [u8] {
35    type Body = Full<Bytes>;
36
37    fn content_type(&self) -> HeaderValue {
38        HeaderValue::from_static(mime::APPLICATION_OCTET_STREAM.as_ref())
39    }
40
41    fn body(self) -> Self::Body {
42        Full::new(self.into())
43    }
44}
45
46impl TypedBody for BytesMut {
47    type Body = Full<Bytes>;
48
49    fn content_type(&self) -> HeaderValue {
50        HeaderValue::from_static(mime::APPLICATION_OCTET_STREAM.as_ref())
51    }
52
53    fn body(self) -> Self::Body {
54        Full::new(self.freeze())
55    }
56}
57
58impl TypedBody for Cow<'static, [u8]> {
59    type Body = Full<Bytes>;
60
61    fn content_type(&self) -> HeaderValue {
62        HeaderValue::from_static(mime::APPLICATION_OCTET_STREAM.as_ref())
63    }
64
65    fn body(self) -> Self::Body {
66        match self {
67            Cow::Borrowed(s) => s.body(),
68            Cow::Owned(s) => s.body(),
69        }
70    }
71}