Skip to main content

sark_http/request/body/
view.rs

1use o3::buffer::{resident, storage};
2
3use crate::{owner::handoff::stage::Body as _, request::body};
4
5/// A contiguous projection retaining its original storage owner.
6#[derive(Clone, Copy)]
7pub enum View<'req, 'd> {
8    Slice(&'req [u8]),
9    Resident(&'req resident::Shared<'d>),
10    Shared(&'req storage::Shared),
11}
12
13impl<'req, 'd> View<'req, 'd> {
14    pub fn as_slice(&self) -> &'req [u8] {
15        match *self {
16            Self::Slice(bytes) => bytes,
17            Self::Resident(owner) => owner.as_slice(),
18            Self::Shared(owner) => owner.as_ref(),
19        }
20    }
21}
22
23impl<'req, 'd> From<View<'req, 'd>> for body::Ref<'req, 'd> {
24    fn from(view: View<'req, 'd>) -> Self {
25        let owner = match view {
26            View::Slice(bytes) => return Self::from_slice(bytes),
27            View::Resident(owner) => body::Retained::Resident(owner),
28            View::Shared(owner) => body::Retained::Shared(owner),
29        };
30        Self {
31            storage: body::RefStorage::Retained(owner),
32            len: owner.defer(),
33        }
34    }
35}
36
37const _: () = {
38    assert!(size_of::<View<'static, 'static>>() == 3 * size_of::<usize>());
39    assert!(size_of::<Option<View<'static, 'static>>>() == size_of::<View<'static, 'static>>());
40};