Skip to main content

sark_http/request/
segments.rs

1use std::{iter::FusedIterator, ops};
2
3use o3::buffer::{bytes::scope, resident, segments::Source};
4
5use crate::{
6    owner::{handoff, handoff::stage},
7    payload::{owned, response},
8    request::{Id, body::Retained},
9};
10
11/// Segmented bytes derived from one exact request.
12#[derive(Debug)]
13pub struct Segments<'d> {
14    body: Source<'d>,
15    id: Id<'d>,
16}
17
18impl<'d> Segments<'d> {
19    #[doc(hidden)]
20    pub const fn new(id: Id<'d>, body: Source<'d>) -> Self {
21        Self { body, id }
22    }
23
24    #[doc(hidden)]
25    pub const fn id(&self) -> Id<'d> {
26        self.id
27    }
28
29    pub fn into_inner(self) -> Source<'d> {
30        self.body
31    }
32
33    pub fn try_clone(&self) -> Result<Self, owned::Error> {
34        Ok(Self {
35            body: Retained::Segments(&self.body).try_clone()?,
36            id: self.id,
37        })
38    }
39}
40
41impl<'d> response::Body<'d> for Segments<'d> {
42    type Bound = resident::bound::Unbounded;
43    type Selected<S, B> = handoff::Selection<'d, S, B>;
44
45    fn len(&self) -> usize {
46        self.body.len()
47    }
48
49    fn as_slice(&self) -> Option<&[u8]> {
50        self.body.contiguous()
51    }
52
53    fn segments(&self) -> impl Iterator<Item = &[u8]> + FusedIterator {
54        self.body.segments()
55    }
56
57    fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
58        self,
59        slot: &handoff::Slot<'req, 'd, H, O>,
60    ) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
61        let Self { body, id } = self;
62        Ok(slot.request(id, body))
63    }
64}
65
66impl<'d> ops::Deref for Segments<'d> {
67    type Target = Source<'d>;
68
69    fn deref(&self) -> &Self::Target {
70        &self.body
71    }
72}