update docs and changelog

This commit is contained in:
Rob Ede 2021-04-13 11:37:05 +01:00
parent c1de2a2eb4
commit 4f11d8983a
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 21 additions and 2 deletions

View File

@ -3,7 +3,7 @@
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
### Added ### Added
* `impl<T: MessageBody> MessageBody for Pin<Box<T>>`. [#2152] * `impl<T: MessageBody> MessageBody for Pin<Box<T>>`. [#2152]
* Helper `body::to_bytes` for async collecting message body into Bytes. [#????] * Helper `body::to_bytes` for async collecting message body into Bytes. [#2158]
### Changes ### Changes
* The type parameter of `Response` no longer has a default. [#2152] * The type parameter of `Response` no longer has a default. [#2152]
@ -23,7 +23,7 @@
[#2065]: https://github.com/actix/actix-web/pull/2065 [#2065]: https://github.com/actix/actix-web/pull/2065
[#2148]: https://github.com/actix/actix-web/pull/2148 [#2148]: https://github.com/actix/actix-web/pull/2148
[#2152]: https://github.com/actix/actix-web/pull/2152 [#2152]: https://github.com/actix/actix-web/pull/2152
[#????]: https://github.com/actix/actix-web/pull/???? [#2158]: https://github.com/actix/actix-web/pull/2158
## 3.0.0-beta.5 - 2021-04-02 ## 3.0.0-beta.5 - 2021-04-02

View File

@ -22,6 +22,25 @@ pub use self::response_body::ResponseBody;
pub use self::size::BodySize; pub use self::size::BodySize;
pub use self::sized_stream::SizedStream; pub use self::sized_stream::SizedStream;
/// Collects the body produced by a `MessageBody` implementation into `Bytes`.
///
/// Any errors produced by the body stream are returned immediately.
///
/// # Examples
/// ```
/// use actix_http::body::{Body, to_bytes};
/// use bytes::Bytes;
///
/// # async fn test_to_bytes() {
/// let body = Body::Empty;
/// let bytes = to_bytes(body).await.unwrap();
/// assert!(bytes.is_empty());
///
/// let body = Body::Bytes(Bytes::from_static(b"123"));
/// let bytes = to_bytes(body).await.unwrap();
/// assert_eq!(bytes, b"123"[..]);
/// # }
/// ```
pub async fn to_bytes(body: impl MessageBody) -> Result<Bytes, crate::Error> { pub async fn to_bytes(body: impl MessageBody) -> Result<Bytes, crate::Error> {
let cap = match body.size() { let cap = match body.size() {
BodySize::None | BodySize::Empty | BodySize::Sized(0) => return Ok(Bytes::new()), BodySize::None | BodySize::Empty | BodySize::Sized(0) => return Ok(Bytes::new()),