Body

Struct Body 

Source
pub struct Body { /* private fields */ }
Expand description

A type that represents an HTTP request or response body.

This type is used to represent the body of an HTTP request/response. It can be either a fixed body (e.g., a string or a byte array) or a streaming body (e.g., a large file or a database query result).

§Examples

use cot::Body;

let body = Body::fixed("Hello, world!");
let body = Body::streaming(futures::stream::once(async { Ok("Hello, world!".into()) }));

Implementations§

Source§

impl Body

Source

pub const fn empty() -> Self

Create an empty body.

§Examples
use cot::Body;

let body = Body::empty();
Source

pub fn fixed<T: Into<Bytes>>(data: T) -> Self

Create a body instance with the given fixed data.

§Examples
use cot::Body;

let body = Body::fixed("Hello, world!");
Source

pub fn streaming<T: Stream<Item = Result<Bytes>> + Send + 'static>( stream: T, ) -> Self

Create a body instance from a stream of data.

§Examples
use async_stream::stream;
use cot::Body;

let stream = stream! {
   yield Ok("Hello, ".into());
   yield Ok("world!".into());
};
let body = Body::streaming(stream);
Source

pub async fn into_bytes(self) -> Result<Bytes>

Convert this Body instance into a byte array.

This method reads the entire body into memory and returns it as a byte array. Note that if the body is too large, this method can consume a lot of memory. For a way to read the body while limiting the memory usage, see Self::into_bytes_limited.

§Errors

This method returns an error if reading the body fails.

§Examples
use cot::Body;

let body = Body::fixed("Hello, world!");
let bytes = body.into_bytes().await?;
assert_eq!(bytes, "Hello, world!".as_bytes());
Source

pub async fn into_bytes_limited(self, limit: usize) -> Result<Bytes>

Convert this Body instance into a byte array.

This is a version of Self::into_bytes that allows you to limit the amount of memory used to read the body. If the body is larger than the limit, an error is returned.

§Errors

This method returns an error if reading the body fails.

If the body is larger than the limit, an error is returned.

§Examples
use cot::Body;

let body = Body::fixed("Hello, world!");
let bytes = body.into_bytes_limited(32).await?;
assert_eq!(bytes, "Hello, world!".as_bytes());

Trait Implementations§

Source§

impl Body for Body

Source§

type Data = Bytes

Values yielded by the Body.
Source§

type Error = Error

The error type this Body might generate.
Source§

fn poll_frame( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>>

Attempt to pull out the next data buffer of this stream.
Source§

fn is_end_stream(&self) -> bool

Returns true when the end of stream has been reached. Read more
Source§

fn size_hint(&self) -> SizeHint

Returns the bounds on the remaining length of the stream. Read more
Source§

impl Debug for Body

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Body

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<&'static [u8]> for Body

Source§

fn from(buf: &'static [u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&'static str> for Body

Source§

fn from(buf: &'static str) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for Body

Source§

fn from(buf: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Body

Source§

fn from(buf: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Body

Source§

fn from(buf: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl IntoResponse for Body

Source§

fn into_response(self) -> Result<Response>

Converts the implementing type into a cot::Result<Response>. Read more
Source§

fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
where K: TryInto<HeaderName>, V: TryInto<HeaderValue>, Self: Sized,

Modifies the response by appending the specified header. Read more
Source§

fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
where V: TryInto<HeaderValue>, Self: Sized,

Modifies the response by setting the Content-Type header. Read more
Source§

fn with_status(self, status: StatusCode) -> WithStatus<Self>
where Self: Sized,

Modifies the response by setting the status code. Read more
Source§

fn with_body(self, body: impl Into<Body>) -> WithBody<Self>
where Self: Sized,

Modifies the response by setting the body. Read more
Source§

fn with_extension<T>(self, extension: T) -> WithExtension<Self, T>
where T: Clone + Send + Sync + 'static, Self: Sized,

Modifies the response by inserting an extension. Read more

Auto Trait Implementations§

§

impl !Freeze for Body

§

impl !RefUnwindSafe for Body

§

impl Send for Body

§

impl Sync for Body

§

impl Unpin for Body

§

impl !UnwindSafe for Body

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> BodyExt for T
where T: Body + ?Sized,

Source§

fn frame(&mut self) -> Frame<'_, Self>
where Self: Unpin,

Returns a future that resolves to the next Frame, if any.
Source§

fn map_frame<F, B>(self, f: F) -> MapFrame<Self, F>
where Self: Sized, F: FnMut(Frame<Self::Data>) -> Frame<B>, B: Buf,

Maps this body’s frame to a different kind.
Source§

fn map_err<F, E>(self, f: F) -> MapErr<Self, F>
where Self: Sized, F: FnMut(Self::Error) -> E,

Maps this body’s error value to a different value.
Source§

fn boxed(self) -> BoxBody<Self::Data, Self::Error>
where Self: Sized + Send + Sync + 'static,

Turn this body into a boxed trait object.
Source§

fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error>
where Self: Sized + Send + 'static,

Turn this body into a boxed trait object that is !Sync.
Source§

fn collect(self) -> Collect<Self>
where Self: Sized,

Turn this body into Collected body which will collect all the DATA frames and trailers.
Source§

fn with_trailers<F>(self, trailers: F) -> WithTrailers<Self, F>
where Self: Sized, F: Future<Output = Option<Result<HeaderMap, Self::Error>>>,

Add trailers to the body. Read more
Source§

fn into_data_stream(self) -> BodyDataStream<Self>
where Self: Sized,

Turn this body into BodyDataStream.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: Rng + ?Sized, Self: FakeBase<U>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoApi for T

Source§

fn into_api<A>(self) -> UseApi<T, A>

into UseApi
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoField<Auto<T>> for T

Source§

fn into_field(self) -> Auto<T>

Available on crate feature db only.
Converts the type to the field type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,