sark-http 0.39.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use o3::buffer::resident;

use crate::request::body;

/// A contiguous projection retaining its original storage owner.
#[derive(Clone, Copy)]
pub enum View<'req, 'd> {
    Slice(&'req [u8]),
    Resident(&'req resident::Shared<'d>),
}

impl<'req, 'd> View<'req, 'd> {
    pub fn as_slice(&self) -> &'req [u8] {
        match *self {
            Self::Slice(bytes) => bytes,
            Self::Resident(owner) => owner.as_slice(),
        }
    }
}

impl<'req, 'd> From<View<'req, 'd>> for body::Ref<'req, 'd> {
    fn from(view: View<'req, 'd>) -> Self {
        let (owner, len) = match view {
            View::Slice(bytes) => return Self::from_slice(bytes),
            View::Resident(owner) => (body::Retained::Resident(owner), owner.len()),
        };
        Self {
            storage: body::RefStorage::Retained(owner),
            len,
        }
    }
}

const _: () = {
    assert!(size_of::<View<'static, 'static>>() <= 3 * size_of::<usize>());
    assert!(size_of::<Option<View<'static, 'static>>>() <= 3 * size_of::<usize>());
};