Skip to main content

sark_http/payload/response/body/
mod.rs

1use std::{iter::FusedIterator, ops::ControlFlow};
2
3use o3::buffer::{bytes::scope, storage, write::Slice};
4
5use crate::{
6    owner::{handoff, handoff::stage},
7    payload::owned,
8};
9
10mod sealed;
11pub(crate) use sealed::Sealed;
12
13/// Statically dispatched response payload.
14pub trait Body<'d>: Sized + Sealed {
15    #[doc(hidden)]
16    const INLINE: bool = false;
17
18    fn len(&self) -> usize;
19
20    fn is_empty(&self) -> bool {
21        self.len() == 0
22    }
23
24    fn as_slice(&self) -> Option<&[u8]>;
25
26    fn segments(&self) -> impl ExactSizeIterator<Item = &[u8]> + FusedIterator {
27        self.as_slice().filter(|body| !body.is_empty()).into_iter()
28    }
29
30    #[doc(hidden)]
31    fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
32        self,
33        slot: &handoff::Slot<'req, 'd, H, O>,
34    ) -> Result<handoff::Plan<'req, 'd, H::Selection, O>, owned::Error>;
35
36    #[doc(hidden)]
37    fn try_resolve_into<
38        'req,
39        H: scope::Source,
40        O: stage::Body<'req, 'd>,
41        L: std::borrow::BorrowMut<usize>,
42    >(
43        self,
44        slot: &handoff::Slot<'req, 'd, H, O>,
45        _: &mut Slice<'_, L>,
46    ) -> Result<ControlFlow<usize, handoff::Plan<'req, 'd, H::Selection, O>>, owned::Error> {
47        self.try_resolve(slot).map(ControlFlow::Continue)
48    }
49}
50
51impl<'d> Body<'d> for [u8; 0] {
52    const INLINE: bool = true;
53
54    fn len(&self) -> usize {
55        <[u8]>::len(self)
56    }
57
58    fn as_slice(&self) -> Option<&[u8]> {
59        Some(self)
60    }
61
62    fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
63        self,
64        slot: &handoff::Slot<'req, 'd, H, O>,
65    ) -> Result<handoff::Plan<'req, 'd, H::Selection, O>, owned::Error> {
66        match self {
67            [] => Ok(slot.clear()),
68        }
69    }
70}
71
72impl<'d, T: AsRef<[u8]> + ?Sized> Body<'d> for &T
73where
74    Self: Sealed,
75{
76    const INLINE: bool = true;
77
78    fn len(&self) -> usize {
79        self.as_ref().len()
80    }
81
82    fn as_slice(&self) -> Option<&[u8]> {
83        Some(self.as_ref())
84    }
85
86    fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
87        self,
88        slot: &handoff::Slot<'req, 'd, H, O>,
89    ) -> Result<handoff::Plan<'req, 'd, H::Selection, O>, owned::Error> {
90        handoff::Plan::try_borrow(slot, self.as_ref())
91    }
92
93    fn try_resolve_into<
94        'req,
95        H: scope::Source,
96        O: stage::Body<'req, 'd>,
97        L: std::borrow::BorrowMut<usize>,
98    >(
99        self,
100        slot: &handoff::Slot<'req, 'd, H, O>,
101        out: &mut Slice<'_, L>,
102    ) -> Result<ControlFlow<usize, handoff::Plan<'req, 'd, H::Selection, O>>, owned::Error> {
103        handoff::Plan::try_borrow_into(slot, self.as_ref(), out)
104    }
105}
106
107impl<'d> Body<'d> for Vec<u8> {
108    const INLINE: bool = true;
109
110    fn len(&self) -> usize {
111        self.as_slice().len()
112    }
113
114    fn as_slice(&self) -> Option<&[u8]> {
115        Some(self.as_slice())
116    }
117
118    fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
119        self,
120        slot: &handoff::Slot<'req, 'd, H, O>,
121    ) -> Result<handoff::Plan<'req, 'd, H::Selection, O>, owned::Error> {
122        Ok(slot.owned(self))
123    }
124}
125
126impl<'d> Body<'d> for String {
127    const INLINE: bool = true;
128
129    fn len(&self) -> usize {
130        String::len(self)
131    }
132
133    fn as_slice(&self) -> Option<&[u8]> {
134        Some(self.as_bytes())
135    }
136
137    fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
138        self,
139        slot: &handoff::Slot<'req, 'd, H, O>,
140    ) -> Result<handoff::Plan<'req, 'd, H::Selection, O>, owned::Error> {
141        Ok(slot.owned(self.into_bytes()))
142    }
143}
144
145impl<'d> Body<'d> for storage::Owned {
146    fn len(&self) -> usize {
147        self.as_ref().len()
148    }
149
150    fn as_slice(&self) -> Option<&[u8]> {
151        Some(self.as_ref())
152    }
153
154    fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
155        self,
156        slot: &handoff::Slot<'req, 'd, H, O>,
157    ) -> Result<handoff::Plan<'req, 'd, H::Selection, O>, owned::Error> {
158        Ok(slot.shared(self.freeze()))
159    }
160}
161
162impl<'d> Body<'d> for storage::Shared {
163    fn len(&self) -> usize {
164        self.as_ref().len()
165    }
166
167    fn as_slice(&self) -> Option<&[u8]> {
168        Some(self.as_ref())
169    }
170
171    fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
172        self,
173        slot: &handoff::Slot<'req, 'd, H, O>,
174    ) -> Result<handoff::Plan<'req, 'd, H::Selection, O>, owned::Error> {
175        Ok(slot.shared(self))
176    }
177}