Skip to main content

sark_http/payload/response/exact/
mod.rs

1use crate::payload::response::status::Code;
2mod overflow;
3mod plan;
4
5use o3::buffer::resident::bound;
6pub use overflow::Overflow;
7pub use plan::Plan;
8
9use crate::payload::response::{Encode, dispatch, headers};
10
11pub trait Body: Encode {
12    fn encoded_len(&self) -> usize;
13}
14
15/// An encoder whose size must be admitted before transport output is prepared.
16pub trait TryBody: Encode {
17    fn try_len(&self) -> Result<usize, Overflow>;
18}
19
20impl<B: Body> TryBody for B {
21    fn try_len(&self) -> Result<usize, Overflow> {
22        Ok(self.encoded_len())
23    }
24}
25
26/// Exact response retaining every generated field in its original type.
27pub struct Direct<H, B> {
28    status: Code,
29    fields: H,
30    body: B,
31}
32
33impl<H, B> Direct<H, B> {
34    pub fn new(status: Code, fields: H, body: B) -> Self {
35        Self {
36            status,
37            fields,
38            body,
39        }
40    }
41
42    pub fn status(&self) -> Code {
43        self.status
44    }
45}
46
47impl<'req, 'd, H, B> dispatch::IntoOutput<'req, 'd> for Direct<H, B>
48where
49    H: headers::Source,
50    B: TryBody,
51{
52    type Output = Self;
53
54    fn into_output(self) -> Self::Output {
55        self
56    }
57}
58
59impl<'req, 'd, H, B> dispatch::Complete<'req, 'd> for Direct<H, B>
60where
61    H: headers::Source,
62    B: TryBody,
63{
64    const BODY: crate::payload::Kind = crate::payload::Kind::Inline;
65    type Bound = bound::Unbounded;
66
67    fn complete<S>(self, sink: S) -> Result<S::Output, S::Error>
68    where
69        S: dispatch::Accept<'d>,
70    {
71        let Self {
72            status,
73            fields,
74            body,
75        } = self;
76        dispatch::Accept::exact(sink, status, fields, body)
77    }
78}