Struct under::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§

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.

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.

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.

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);
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§

Formats the value using the given formatter. Read more
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more