http-kit 0.4.2

A flexible and ergonomic HTTP toolkit for Rust with async support, middleware, and zero-copy body handling
Documentation
use alloc::{borrow::Cow, boxed::Box, string::String, vec::Vec};
use bytes::Bytes;
use bytestr::ByteStr;
use core::pin::Pin;
use futures_lite::AsyncBufRead;

use super::{Body, BodyInner};

macro_rules! from_bytes {
    ($($ty:ty),*) => {
        $(
            impl From<$ty> for Body {
                fn from(data: $ty) -> Self {
                    Body::from_bytes(data)
                }
            }
        )*
    };
}

macro_rules! from_str {
    ($($ty:ty),*) => {
        $(
            impl From<$ty> for Body {
                fn from(data: $ty) -> Self {
                    Body::from_text(data)
                }
            }
        )*
    };
}

from_str!(ByteStr, String, Box<str>, &str, Cow<'_, str>);

from_bytes!(Bytes, Vec<u8>, Box<[u8]>);

impl<'a> From<Cow<'a, [u8]>> for Body {
    fn from(data: Cow<[u8]>) -> Self {
        Body::from_bytes(data.into_owned())
    }
}

impl From<&[u8]> for Body {
    fn from(data: &[u8]) -> Self {
        Body::from_bytes(data.to_vec())
    }
}

impl From<Box<dyn AsyncBufRead + Send + Sync + 'static>> for Body {
    fn from(reader: Box<dyn AsyncBufRead + Send + Sync + 'static>) -> Self {
        Pin::from(reader).into()
    }
}

impl From<Pin<Box<dyn AsyncBufRead + Send + Sync + 'static>>> for Body {
    fn from(reader: Pin<Box<dyn AsyncBufRead + Send + Sync + 'static>>) -> Self {
        Self {
            mime: None,
            inner: BodyInner::Reader {
                reader,
                length: None,
            },
        }
    }
}