Skip to main content

sark_http/request/
segments.rs

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