Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/http/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub mod util {
/// * `async fn Body::contents(&mut self) -> Result<&[u8], Error>` is ready
/// when all contents of the body have been collected, and gives them as a
/// byte slice.
/// * `async fn Body::bytes_contents(&mut self) -> Result<Bytes, Error>` is
/// the same as `Body::contents`, except returns the `Bytes` type instead of
/// a byte slice.
/// * `async fn Body::str_contents(&mut self) -> Result<&str, Error>` is ready
/// when all contents of the body have been collected, and gives them as a str
/// slice.
Expand Down Expand Up @@ -170,6 +173,16 @@ impl Body {
}
}
}
/// Collect the entire contents of this `Body`, and expose it as `Bytes`.
/// This async fn will be pending until the entire `Body` is
/// copied into memory, or an error occurs.
pub async fn bytes_contents(&mut self) -> Result<Bytes, Error> {
let _ = self.contents().await?;
match &self.0 {
BodyInner::Complete { data, .. } => Ok(data.clone()),
_ => unreachable!(),
}
}

/// Get a value for the length of this `Body`'s content, in bytes, if
/// known. This value can come from either the Content-Length header
Expand Down
Loading