1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-30 08:38:16 +02:00

ClientRequest::send_body takes impl MessageBody (#2546)

This commit is contained in:
Rob Ede
2021-12-25 02:33:37 +00:00
committed by GitHub
parent 1296e07c48
commit d2590fd46c
18 changed files with 853 additions and 687 deletions

View File

@@ -77,10 +77,27 @@ impl<B> AnyBody<B>
where
B: MessageBody + 'static,
{
/// Converts a [`MessageBody`] type into the best possible representation.
///
/// Checks size for `None` and tries to convert to `Bytes`. Otherwise, uses the `Body` variant.
pub fn from_message_body(body: B) -> Self
where
B: MessageBody,
{
if matches!(body.size(), BodySize::None) {
return Self::None;
}
match body.try_into_bytes() {
Ok(body) => Self::Bytes { body },
Err(body) => Self::new(body),
}
}
pub fn into_boxed(self) -> AnyBody {
match self {
Self::None => AnyBody::None,
Self::Bytes { body: bytes } => AnyBody::Bytes { body: bytes },
Self::Bytes { body } => AnyBody::Bytes { body },
Self::Body { body } => AnyBody::new_boxed(body),
}
}