Skip to main content

sark_http/payload/fixed/
body.rs

1use o3::buffer::bytes::scope;
2
3use crate::{
4    owner::{handoff, handoff::stage},
5    payload::{owned, response},
6};
7
8#[derive(Clone, Copy, Eq, PartialEq)]
9#[repr(transparent)]
10pub struct Body<'a> {
11    bytes: &'a [u8],
12}
13
14impl<'a> Body<'a> {
15    pub const fn new(bytes: &'a [u8]) -> Self {
16        Self { bytes }
17    }
18
19    pub const fn as_slice(&self) -> &'a [u8] {
20        self.bytes
21    }
22
23    pub const fn len(self) -> usize {
24        self.bytes.len()
25    }
26
27    pub const fn is_empty(self) -> bool {
28        self.bytes.is_empty()
29    }
30}
31
32impl<'body_d, 'd> response::Body<'d> for Body<'body_d>
33where
34    'body_d: 'd,
35{
36    const INLINE: bool = true;
37
38    fn len(&self) -> usize {
39        self.bytes.len()
40    }
41
42    fn as_slice(&self) -> Option<&[u8]> {
43        Some(self.bytes)
44    }
45
46    fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
47        self,
48        slot: &handoff::Slot<'req, 'd, H, O>,
49    ) -> Result<handoff::Plan<'req, 'd, H::Selection, O>, owned::Error> {
50        Ok(handoff::Plan::new(slot.origin().fixed(self)))
51    }
52}
53
54impl AsRef<[u8]> for Body<'_> {
55    fn as_ref(&self) -> &[u8] {
56        self.bytes
57    }
58}