sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
/// # Safety
/// `out` must admit two writable bytes and `value` must be below 100.
pub(crate) const unsafe fn pair(out: *mut u8, value: usize) {
    // SAFETY: the caller supplies both ranges; a byte pair has alignment one.
    unsafe {
        out.cast::<[u8; 2]>()
            .write(*super::PAIRS.as_ptr().add(value))
    };
}

pub(super) const fn write<const N: usize>(mut value: u64, buf: &mut [u8; N]) -> &[u8] {
    const { assert!(N == 10 || N == 20) };
    let mut start = N;
    let out = buf.as_mut_ptr();
    while value >= 100 {
        let (rem, next) = if N == 10 || usize::BITS <= 32 {
            let word = value as u32;
            ((word % 100) as usize, (word / 100) as u64)
        } else {
            ((value % 100) as usize, value / 100)
        };
        start -= 2;
        // SAFETY: the fixed buffer covers every digit and the remainder is below 100.
        unsafe { pair(out.add(start), rem) };
        value = next;
    }
    if value < 10 {
        start -= 1;
        // SAFETY: the fixed buffer has room for the leading digit.
        unsafe { out.add(start).write(b'0' + value as u8) };
    } else {
        start -= 2;
        // SAFETY: the leading pair is below 100 and fits the remaining prefix.
        unsafe { pair(out.add(start), value as usize) };
    }
    // SAFETY: start only moved across the suffix just written in this array.
    unsafe { buf.split_at_unchecked(start).1 }
}