Skip to main content

sark_http/request/body/
view.rs

1use o3::buffer::resident;
2
3use crate::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}
11
12impl<'req, 'd> View<'req, 'd> {
13    pub fn as_slice(&self) -> &'req [u8] {
14        match *self {
15            Self::Slice(bytes) => bytes,
16            Self::Resident(owner) => owner.as_slice(),
17        }
18    }
19}
20
21impl<'req, 'd> From<View<'req, 'd>> for body::Ref<'req, 'd> {
22    fn from(view: View<'req, 'd>) -> Self {
23        let (owner, len) = match view {
24            View::Slice(bytes) => return Self::from_slice(bytes),
25            View::Resident(owner) => (body::Retained::Resident(owner), owner.len()),
26        };
27        Self {
28            storage: body::RefStorage::Retained(owner),
29            len,
30        }
31    }
32}
33
34const _: () = {
35    assert!(size_of::<View<'static, 'static>>() <= 3 * size_of::<usize>());
36    assert!(size_of::<Option<View<'static, 'static>>>() <= 3 * size_of::<usize>());
37};