sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use o3::buffer::{
    pool,
    pool::{CreateError, LayoutError},
};

use crate::h1::switch;

mod buf;
mod meta;
mod open;

pub use buf::Buf;
pub use meta::Meta;
pub use open::Open;

#[derive(Clone, Copy)]
#[repr(C)]
pub(in crate::h1::head::request) struct Layout {
    pub(in crate::h1::head::request) method: u16,
    pub(in crate::h1::head::request) end: u16,
    pub(in crate::h1::head::request) head: u16,
    pub(in crate::h1::head::request) intent: switch::Intent,
}

impl Layout {
    pub(in crate::h1::head::request) const BYTES: usize = size_of::<Self>();
    const MAX: usize = (1 << 13) - 1;

    pub(in crate::h1::head::request) fn encode(self) -> [u8; Self::BYTES] {
        let [a, b] = self.method.to_ne_bytes();
        let [c, d] = self.end.to_ne_bytes();
        let [e, f] = self.head.to_ne_bytes();
        [a, b, c, d, e, f, self.intent as u8, 0]
    }
}

pub struct Pool {
    inner: pool::Pool,
}

impl Pool {
    pub fn try_new(slots: usize, capacity: usize) -> Result<Self, CreateError> {
        if capacity > Layout::MAX {
            return Err(CreateError::Layout(LayoutError::CapacityTooLarge));
        }
        Ok(Self {
            inner: pool::Pool::try_new(slots, capacity + Layout::BYTES)?,
        })
    }

    pub fn open(&self) -> Option<Open<'_>> {
        self.inner.borrow().map(Open::new)
    }

    pub fn slots(&self) -> usize {
        self.inner.slots()
    }

    pub fn available(&self) -> usize {
        self.inner.available()
    }
}

const _: () = assert!(size_of::<Layout>() == 8);
const _: () = assert!(size_of::<Pool>() == size_of::<usize>());