http_body_request_validator/convert.rs
1//! Conversion traits and utils.
2
3/// Conversion of a buffered type into a body.
4pub trait BufferedToBody {
5 /// The buffered body parts type to convert from.
6 type Buffered;
7
8 /// The body type to convert to.
9 type Body: http_body::Body;
10
11 /// Consume buffered and turn it into a body.
12 fn buffered_to_body(buffered: Self::Buffered) -> Self::Body;
13}
14
15/// Convert self into body.
16pub trait IntoBody {
17 /// The body type to convert to.
18 type Body: http_body::Body;
19
20 /// Consume self and turn into a body.
21 fn into_body(self) -> Self::Body;
22}
23
24/// The default [`BufferedToBody`] implementation that works for all [`IntoBody`] implementations.
25///
26/// Swap this type with your own to alter the type of body that the conversion targets.
27///
28/// This type in not intended to be instantiated.
29pub struct Trivial<Buffered>(pub(crate) core::marker::PhantomData<Buffered>);
30
31impl<Buffered> BufferedToBody for Trivial<Buffered>
32where
33 Buffered: IntoBody,
34{
35 type Buffered = Buffered;
36 type Body = <Buffered as IntoBody>::Body;
37
38 fn buffered_to_body(buffered: Buffered) -> Self::Body {
39 buffered.into_body()
40 }
41}