1use std::{iter::FusedIterator, ops::ControlFlow};
2
3use o3::buffer::{bytes::scope, resident, segments::Source, storage, write::Slice};
4
5use crate::{
6 owner::{handoff, handoff::stage},
7 payload::owned,
8};
9
10mod sealed;
11pub(crate) use sealed::Sealed;
12
13pub trait Body<'d>: Sized + Sealed {
15 type Bound: resident::bound::Bound + 'd;
16 type Selected<S, B>: Into<handoff::Selection<'d, S, B>>;
17
18 #[doc(hidden)]
19 const INLINE: bool = false;
20
21 fn len(&self) -> usize;
22
23 fn is_empty(&self) -> bool {
24 self.len() == 0
25 }
26
27 fn as_slice(&self) -> Option<&[u8]>;
28
29 fn segments(&self) -> impl Iterator<Item = &[u8]> + FusedIterator {
30 self.as_slice().filter(|body| !body.is_empty()).into_iter()
31 }
32
33 #[doc(hidden)]
34 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
35 self,
36 slot: &handoff::Slot<'req, 'd, H, O>,
37 ) -> Result<Self::Selected<H::Selection, O>, owned::Error>;
38
39 #[doc(hidden)]
40 fn try_resolve_into<
41 'req,
42 H: scope::Source,
43 O: stage::Body<'req, 'd>,
44 L: std::borrow::BorrowMut<usize>,
45 >(
46 self,
47 slot: &handoff::Slot<'req, 'd, H, O>,
48 _: &mut Slice<'_, L>,
49 ) -> Result<ControlFlow<usize, Self::Selected<H::Selection, O>>, owned::Error> {
50 Ok(ControlFlow::Continue(self.try_resolve(slot)?))
51 }
52}
53
54impl<'d> Body<'d> for [u8; 0] {
55 type Bound = resident::bound::AtMost<0>;
56 type Selected<S, B> = handoff::Selection<'d, S, B>;
57
58 const INLINE: bool = true;
59
60 fn len(&self) -> usize {
61 <[u8]>::len(self)
62 }
63
64 fn as_slice(&self) -> Option<&[u8]> {
65 Some(self)
66 }
67
68 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
69 self,
70 slot: &handoff::Slot<'req, 'd, H, O>,
71 ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
72 match self {
73 [] => Ok(slot.clear()),
74 }
75 }
76}
77
78impl<'d, T: AsRef<[u8]> + ?Sized> Body<'d> for &T
79where
80 Self: Sealed,
81{
82 type Bound = resident::bound::Unbounded;
83 type Selected<S, B> = handoff::Selection<'d, S, B>;
84
85 const INLINE: bool = true;
86
87 fn len(&self) -> usize {
88 self.as_ref().len()
89 }
90
91 fn as_slice(&self) -> Option<&[u8]> {
92 Some(self.as_ref())
93 }
94
95 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
96 self,
97 slot: &handoff::Slot<'req, 'd, H, O>,
98 ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
99 handoff::Selection::try_borrow(slot, self.as_ref())
100 }
101
102 fn try_resolve_into<
103 'req,
104 H: scope::Source,
105 O: stage::Body<'req, 'd>,
106 L: std::borrow::BorrowMut<usize>,
107 >(
108 self,
109 slot: &handoff::Slot<'req, 'd, H, O>,
110 out: &mut Slice<'_, L>,
111 ) -> Result<ControlFlow<usize, handoff::Selection<'d, H::Selection, O>>, owned::Error> {
112 handoff::Selection::try_borrow_into(slot, self.as_ref(), out)
113 }
114}
115
116impl<'d> Body<'d> for Vec<u8> {
117 type Bound = resident::bound::Unbounded;
118 type Selected<S, B> = Option<storage::NonEmpty<Vec<u8>>>;
119
120 const INLINE: bool = true;
121
122 fn len(&self) -> usize {
123 self.as_slice().len()
124 }
125
126 fn as_slice(&self) -> Option<&[u8]> {
127 Some(self.as_slice())
128 }
129
130 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
131 self,
132 _: &handoff::Slot<'req, 'd, H, O>,
133 ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
134 Ok(match storage::NonEmpty::try_new(self) {
135 Ok(body) => Some(body),
136 Err(empty) => {
137 drop(empty);
138 None
139 }
140 })
141 }
142}
143
144impl<'d> Body<'d> for storage::NonEmpty<Vec<u8>> {
145 type Bound = resident::bound::Unbounded;
146 type Selected<S, B> = Self;
147
148 const INLINE: bool = true;
149
150 fn len(&self) -> usize { storage::NonEmpty::len(self).get() }
151 fn is_empty(&self) -> bool { false }
152 fn as_slice(&self) -> Option<&[u8]> { Some(self.as_ref()) }
153
154 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
155 self,
156 _: &handoff::Slot<'req, 'd, H, O>,
157 ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
158 Ok(self)
159 }
160}
161
162impl<'d> Body<'d> for String {
163 type Bound = resident::bound::Unbounded;
164 type Selected<S, B> = Option<storage::NonEmpty<Vec<u8>>>;
165
166 const INLINE: bool = true;
167
168 fn len(&self) -> usize {
169 String::len(self)
170 }
171
172 fn as_slice(&self) -> Option<&[u8]> {
173 Some(self.as_bytes())
174 }
175
176 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
177 self,
178 slot: &handoff::Slot<'req, 'd, H, O>,
179 ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
180 self.into_bytes().try_resolve(slot)
181 }
182}
183
184impl<'d> Body<'d> for storage::Owned {
185 type Bound = resident::bound::Unbounded;
186 type Selected<S, B> = handoff::Selection<'d, S, B>;
187
188 fn len(&self) -> usize {
189 self.as_ref().len()
190 }
191
192 fn as_slice(&self) -> Option<&[u8]> {
193 Some(self.as_ref())
194 }
195
196 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
197 self,
198 slot: &handoff::Slot<'req, 'd, H, O>,
199 ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
200 Ok(slot.shared(self.freeze()))
201 }
202}
203
204impl<'d> Body<'d> for storage::Shared {
205 type Bound = resident::bound::Unbounded;
206 type Selected<S, B> = handoff::Selection<'d, S, B>;
207
208 fn len(&self) -> usize {
209 self.as_ref().len()
210 }
211
212 fn as_slice(&self) -> Option<&[u8]> {
213 Some(self.as_ref())
214 }
215
216 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
217 self,
218 slot: &handoff::Slot<'req, 'd, H, O>,
219 ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
220 Ok(slot.shared(self))
221 }
222}
223
224impl<'d> Body<'d> for resident::Shared<'d> {
225 type Bound = resident::bound::Unbounded;
226 type Selected<S, B> = handoff::Selection<'d, S, B>;
227
228 fn len(&self) -> usize {
229 self.as_slice().len()
230 }
231
232 fn as_slice(&self) -> Option<&[u8]> {
233 Some(self.as_slice())
234 }
235
236 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
237 self,
238 slot: &handoff::Slot<'req, 'd, H, O>,
239 ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
240 Ok(slot.segmented(Source::from(self)))
241 }
242}
243
244impl<'d> Body<'d> for Source<'d> {
245 type Bound = resident::bound::Unbounded;
246 type Selected<S, B> = handoff::Selection<'d, S, B>;
247
248 fn len(&self) -> usize {
249 Source::len(self)
250 }
251
252 fn as_slice(&self) -> Option<&[u8]> {
253 self.contiguous()
254 }
255
256 fn segments(&self) -> impl Iterator<Item = &[u8]> + FusedIterator {
257 Source::segments(self)
258 }
259
260 fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
261 self,
262 slot: &handoff::Slot<'req, 'd, H, O>,
263 ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
264 Ok(slot.segmented(self))
265 }
266}