sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use std::ops::ControlFlow;

use dope_test::alloc::Tracker;
use o3::{
    alloc,
    buffer::{storage, write},
};
use sark_http::payload::{owned, response::Body};

#[global_allocator]
static ALLOCATOR: Tracker = Tracker::new();

#[test]
fn copies_within_capacity() {
    let source = *b"abcde";
    let mut input = source.as_slice();
    let mut out = [0; 5];
    Tracker::fail_after(0, || {
        let result = <&[u8] as Body<'_>>::try_inline(
            source.as_slice(),
            &mut write::raw::Slice::new(&mut out),
        );
        assert!(matches!(result, Ok(ControlFlow::Break(5))));
        let result =
            <&mut &[u8] as Body<'_>>::try_inline(&mut input, &mut write::raw::Slice::new(&mut out));
        assert!(matches!(result, Ok(ControlFlow::Break(5))));
        assert_eq!(out, source);
        assert!(matches!(
            <&[u8] as Body<'_>>::try_inline(
                source.as_slice(),
                &mut write::raw::Slice::new(&mut out[..4]),
            ),
            Err(owned::Error::Storage(storage::AllocError::Exhausted(
                alloc::Exhausted
            )))
        ));
        assert!(matches!(
            Tracker::fail_after(0, || {
                <&mut &[u8] as Body<'_>>::try_inline(
                    &mut input,
                    &mut write::raw::Slice::new(&mut out[..4]),
                )
            }),
            Err(owned::Error::Build(storage::BuildError::Exhausted(
                alloc::Exhausted
            )))
        ));
        assert!(matches!(
            <&[u8] as Body<'_>>::try_inline(&[], &mut write::raw::Slice::new(&mut out)),
            Ok(ControlFlow::Continue(None))
        ));
    });
}

#[test]
fn transfers_owned_buffer() {
    let source = Vec::from(b"abcde".as_slice());
    let pointer = source.as_ptr();
    let mut out = [0; 5];
    let (result, counts) = Tracker::counts(|| {
        <Vec<u8> as Body<'_>>::try_inline(source, &mut write::raw::Slice::new(&mut out))
    });
    let body = result
        .expect("owned placement")
        .continue_value()
        .expect("owned storage")
        .expect("owned body");
    assert_eq!(counts, [0, 0]);
    assert_eq!(out, [0; 5]);
    assert_eq!(body.as_ref().as_ptr(), pointer);
}