sark-http 0.39.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use std::{borrow::Borrow, convert::Infallible, ptr};

use o3::buffer::{
    self, hold, resident, segments,
    storage::backing,
};
use segments::Source;

use crate::{
    owner::handoff::stage,
    payload::owned,
};

#[derive(Clone, Copy, Debug)]
pub enum Retained<'req, 'd> {
    Segments(&'req Source<'d>),
    Resident(&'req resident::Shared<'d>),
}

impl<'req, 'd> Retained<'req, 'd> {
    pub(crate) fn count(self) -> segments::Count {
        match self {
            Self::Segments(owner) => owner.count(),
            Self::Resident(owner) => segments::Count::new(u32::from(!owner.is_empty())),
        }
    }

    pub(crate) fn segment(self, index: usize) -> Option<&'req [u8]> {
        match self {
            Self::Segments(owner) => owner.segments().nth(index),
            Self::Resident(owner) => (index == 0 && !owner.is_empty()).then(|| owner.as_slice()),
        }
    }

    pub(crate) fn as_slice(&self) -> Option<&'req [u8]> {
        match *self {
            Self::Segments(owner) => owner.contiguous(),
            Self::Resident(owner) => Some(owner.as_slice()),
        }
    }

    pub(crate) fn is_same(self, other: Self) -> bool {
        match (self, other) {
            (Self::Segments(left), Self::Segments(right)) => ptr::eq(left, right),
            (Self::Resident(left), Self::Resident(right)) => ptr::eq(left, right),
            _ => false,
        }
    }

    pub fn charge(self) -> hold::Charge {
        match self {
            Self::Segments(owner) => hold::Charge::from(owner),
            Self::Resident(owner) => owner
                .backing()
                .map_or_else(hold::Charge::default, |backing| {
                    hold::Charge::from_owner(owner.len(), backing.resident())
                }),
        }
    }

    pub(crate) fn contains(self, response: backing::View<'_, '_>) -> bool {
        match self {
            Self::Segments(owner) => owner.contains(&response),
            Self::Resident(owner) => owner
                .backing()
                .is_some_and(|backing| backing.contains(&response)),
        }
    }

    pub fn try_clone(self) -> Result<Source<'d>, owned::Error> {
        match self {
            Self::Segments(owner) => owner.try_clone().map_err(|error| match error {
                hold::ranges::retain::Error::Metadata(error) => owned::Error::Metadata(error),
                hold::ranges::retain::Error::Retain(error) => owned::Error::Retain(error),
            }),
            Self::Resident(owner) => owner.try_clone().map(Source::from).map_err(owned::Error::Retain),
        }
    }

    pub(crate) fn try_slice(
        self,
        bytes: &[u8],
    ) -> Result<Option<Source<'d>>, owned::Error> {
        match self {
            Self::Segments(owner) => owner.try_slice(bytes).map_err(owned::Error::Retain),
            Self::Resident(owner) => buffer::View::new(owner)
                .select(bytes)
                .map(|view| view.try_retain().map(Source::from).map_err(owned::Error::Retain))
                .transpose(),
        }
    }
}

const _: () = assert!(size_of::<Retained<'static, 'static>>() == 2 * size_of::<usize>());

impl<'req, 'd: 'req> stage::Body<'req, 'd> for Retained<'req, 'd> {
    type Deferred = ();

    fn defer(self) {}
}
impl<'req, 'd: 'req> Borrow<Retained<'req, 'd>> for Infallible {
    fn borrow(&self) -> &Retained<'req, 'd> {
        match *self {}
    }
}