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§
source§impl DataStream
impl DataStream
sourcepub async fn into<W: AsyncWrite + Unpin>(
self,
writer: &mut W
) -> Result<DataTransfer, UnderError>
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.
sourcepub async fn into_bytes(self) -> Result<Vec<u8>, UnderError>
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.
sourcepub async fn into_text(self) -> Result<String, UnderError>
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.
sourcepub async fn into_json<T: DeserializeOwned>(self) -> Result<T, UnderError>
Available on crate feature json only.
pub async fn into_json<T: DeserializeOwned>(self) -> Result<T, UnderError>
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);sourcepub async fn into_form<T: FromForm>(self) -> Result<T, UnderError>
Available on crate feature from_form only.
pub async fn into_form<T: FromForm>(self) -> Result<T, UnderError>
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);