DataStream

Struct DataStream 

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

The data stream of a body.

This should be used to read and write data to the body. There are always implicit limits to streaming data, the only difference is whether or not your code is prepared to handle that limit.

This allows us to perform operations on a request/response body without having to worry about data limits.

Implementations§

Source§

impl DataStream

Source

pub async fn into<W: AsyncWrite + Unpin>( self, writer: &mut W, ) -> Result<DataTransfer, UnderError>

Read data from the stream.

This streams from the body into the provided writer, and returns the number of bytes read and whether or not the stream is complete.

§Errors

This returns an error if the underlying stream cannot be written to the given writer. It does not return an error if the stream is incomplete, as that is expected to be handled by the caller.

Source

pub async fn into_bytes(self) -> Result<Vec<u8>, UnderError>

Read data from the stream into a byte array.

This streams from the body into the provided buffer, and returns the resulting buffer. If the body of the request is too large to fit into the limit of the buffer, then an error is returned.

§Errors

This returns an error if the underlying stream cannot be written to a buffer, or if the stream is incomplete.

Source

pub async fn into_text(self) -> Result<String, UnderError>

Read data from the stream into a string.

This streams from the body into the provided buffer, and returns the resulting buffer. If the body of the request is too large to fit into the limit of the buffer, then an error is returned.

§Errors

Errors for the same reason as DataStream::into_bytes, and also returns an error if the body cannot be converted to a UTF-8 string.

Source

pub async fn into_json<T: DeserializeOwned>(self) -> Result<T, UnderError>

Available on crate feature json only.

Parses the contents of the body as JSON, deserializing it into the given value. JSON has strict limits on the bytes/characters allowed for serialization/deserialization, so the charset should not matter.

§Errors

Errors for the same reason as DataStream::into_bytes, and also returns an error if the body cannot be converted to a JSON value.

§Examples
let stream = DataStream::from(r#"{"hello": "world"}"#);
dbg!(&stream);
let body = stream.into_json::<serde_json::Value>().await.unwrap();
let expected = serde_json::json!({ "hello": "world" });
assert_eq!(body, expected);
Source

pub async fn into_form<T: FromForm>(self) -> Result<T, UnderError>

Available on crate feature from_form only.

Parses the contents of the body as x-www-form-urlencoded, deserializing it into the given value. This assumes that the request body is already UTF-8, or a UTF-8 compatible encoding, and does not check the content-type to make sure. If that is a concern, use Self::into_bytes, and handle the conversion yourself; or, if it’s a common occurrence, open a ticket, with your use-case and a proposed solution.

§Errors

Errors for the same reason as DataStream::into_bytes, and also returns an error if the body cannot be converted to a form value.

§Examples
let stream = DataStream::from(r#"hello=world"#);
let body = stream.into_form::<HashMap<String, Vec<String>>>().await?;
assert_eq!(&body["hello"][..], &["world".to_string()]);
assert_eq!(body.len(), 1);

Trait Implementations§

Source§

impl Debug for DataStream

Source§

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

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

impl<T> From<T> for DataStream
where T: Into<Body>,

Source§

fn from(body: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> 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> 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, 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,