1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-23 01:51:11 +00:00

inline trivial body methods

This commit is contained in:
Rob Ede 2021-12-11 16:05:08 +00:00
parent 5b0a50249b
commit b41b346c00
No known key found for this signature in database
GPG key ID: 97C636207D3EF933
6 changed files with 42 additions and 5 deletions

View file

@ -27,6 +27,7 @@ where
S: Stream<Item = Result<Bytes, E>>, S: Stream<Item = Result<Bytes, E>>,
E: Into<Box<dyn StdError>> + 'static, E: Into<Box<dyn StdError>> + 'static,
{ {
#[inline]
pub fn new(stream: S) -> Self { pub fn new(stream: S) -> Self {
BodyStream { stream } BodyStream { stream }
} }
@ -39,6 +40,7 @@ where
{ {
type Error = E; type Error = E;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Stream BodySize::Stream
} }

View file

@ -23,6 +23,7 @@ pin_project! {
impl<L> EitherBody<L, BoxBody> { impl<L> EitherBody<L, BoxBody> {
/// Creates new `EitherBody` using left variant and boxed right variant. /// Creates new `EitherBody` using left variant and boxed right variant.
#[inline]
pub fn new(body: L) -> Self { pub fn new(body: L) -> Self {
Self::Left { body } Self::Left { body }
} }
@ -30,11 +31,13 @@ impl<L> EitherBody<L, BoxBody> {
impl<L, R> EitherBody<L, R> { impl<L, R> EitherBody<L, R> {
/// Creates new `EitherBody` using left variant. /// Creates new `EitherBody` using left variant.
#[inline]
pub fn left(body: L) -> Self { pub fn left(body: L) -> Self {
Self::Left { body } Self::Left { body }
} }
/// Creates new `EitherBody` using right variant. /// Creates new `EitherBody` using right variant.
#[inline]
pub fn right(body: R) -> Self { pub fn right(body: R) -> Self {
Self::Right { body } Self::Right { body }
} }
@ -47,6 +50,7 @@ where
{ {
type Error = Error; type Error = Error;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
match self { match self {
EitherBody::Left { body } => body.size(), EitherBody::Left { body } => body.size(),
@ -54,6 +58,7 @@ where
} }
} }
#[inline]
fn poll_next( fn poll_next(
self: Pin<&mut Self>, self: Pin<&mut Self>,
cx: &mut Context<'_>, cx: &mut Context<'_>,
@ -68,6 +73,7 @@ where
} }
} }
#[inline]
fn is_complete_body(&self) -> bool { fn is_complete_body(&self) -> bool {
match self { match self {
EitherBody::Left { body } => body.is_complete_body(), EitherBody::Left { body } => body.is_complete_body(),
@ -75,6 +81,7 @@ where
} }
} }
#[inline]
fn take_complete_body(&mut self) -> Bytes { fn take_complete_body(&mut self) -> Bytes {
match self { match self {
EitherBody::Left { body } => body.take_complete_body(), EitherBody::Left { body } => body.take_complete_body(),

View file

@ -85,12 +85,10 @@ mod foreign_impls {
impl MessageBody for Infallible { impl MessageBody for Infallible {
type Error = Infallible; type Error = Infallible;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
match *self {} match *self {}
} }
#[inline]
fn poll_next( fn poll_next(
self: Pin<&mut Self>, self: Pin<&mut Self>,
_cx: &mut Context<'_>, _cx: &mut Context<'_>,
@ -219,6 +217,7 @@ mod foreign_impls {
impl MessageBody for &'static [u8] { impl MessageBody for &'static [u8] {
type Error = Infallible; type Error = Infallible;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
} }
@ -234,10 +233,12 @@ mod foreign_impls {
} }
} }
#[inline]
fn is_complete_body(&self) -> bool { fn is_complete_body(&self) -> bool {
true true
} }
#[inline]
fn take_complete_body(&mut self) -> Bytes { fn take_complete_body(&mut self) -> Bytes {
Bytes::from_static(mem::take(self)) Bytes::from_static(mem::take(self))
} }
@ -246,6 +247,7 @@ mod foreign_impls {
impl MessageBody for Bytes { impl MessageBody for Bytes {
type Error = Infallible; type Error = Infallible;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
} }
@ -261,10 +263,12 @@ mod foreign_impls {
} }
} }
#[inline]
fn is_complete_body(&self) -> bool { fn is_complete_body(&self) -> bool {
true true
} }
#[inline]
fn take_complete_body(&mut self) -> Bytes { fn take_complete_body(&mut self) -> Bytes {
mem::take(self) mem::take(self)
} }
@ -273,6 +277,7 @@ mod foreign_impls {
impl MessageBody for BytesMut { impl MessageBody for BytesMut {
type Error = Infallible; type Error = Infallible;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
} }
@ -288,10 +293,12 @@ mod foreign_impls {
} }
} }
#[inline]
fn is_complete_body(&self) -> bool { fn is_complete_body(&self) -> bool {
true true
} }
#[inline]
fn take_complete_body(&mut self) -> Bytes { fn take_complete_body(&mut self) -> Bytes {
mem::take(self).freeze() mem::take(self).freeze()
} }
@ -300,6 +307,7 @@ mod foreign_impls {
impl MessageBody for Vec<u8> { impl MessageBody for Vec<u8> {
type Error = Infallible; type Error = Infallible;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
} }
@ -315,10 +323,12 @@ mod foreign_impls {
} }
} }
#[inline]
fn is_complete_body(&self) -> bool { fn is_complete_body(&self) -> bool {
true true
} }
#[inline]
fn take_complete_body(&mut self) -> Bytes { fn take_complete_body(&mut self) -> Bytes {
Bytes::from(mem::take(self)) Bytes::from(mem::take(self))
} }
@ -327,6 +337,7 @@ mod foreign_impls {
impl MessageBody for &'static str { impl MessageBody for &'static str {
type Error = Infallible; type Error = Infallible;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
} }
@ -344,10 +355,12 @@ mod foreign_impls {
} }
} }
#[inline]
fn is_complete_body(&self) -> bool { fn is_complete_body(&self) -> bool {
true true
} }
#[inline]
fn take_complete_body(&mut self) -> Bytes { fn take_complete_body(&mut self) -> Bytes {
Bytes::from_static(mem::take(self).as_bytes()) Bytes::from_static(mem::take(self).as_bytes())
} }
@ -356,6 +369,7 @@ mod foreign_impls {
impl MessageBody for String { impl MessageBody for String {
type Error = Infallible; type Error = Infallible;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
} }
@ -372,10 +386,12 @@ mod foreign_impls {
} }
} }
#[inline]
fn is_complete_body(&self) -> bool { fn is_complete_body(&self) -> bool {
true true
} }
#[inline]
fn take_complete_body(&mut self) -> Bytes { fn take_complete_body(&mut self) -> Bytes {
Bytes::from(mem::take(self)) Bytes::from(mem::take(self))
} }
@ -384,6 +400,7 @@ mod foreign_impls {
impl MessageBody for bytestring::ByteString { impl MessageBody for bytestring::ByteString {
type Error = Infallible; type Error = Infallible;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
} }
@ -396,10 +413,12 @@ mod foreign_impls {
Poll::Ready(Some(Ok(string.into_bytes()))) Poll::Ready(Some(Ok(string.into_bytes())))
} }
#[inline]
fn is_complete_body(&self) -> bool { fn is_complete_body(&self) -> bool {
true true
} }
#[inline]
fn take_complete_body(&mut self) -> Bytes { fn take_complete_body(&mut self) -> Bytes {
mem::take(self).into_bytes() mem::take(self).into_bytes()
} }
@ -435,6 +454,7 @@ where
{ {
type Error = E; type Error = E;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
self.body.size() self.body.size()
} }

View file

@ -1,9 +1,11 @@
/// Body size hint. /// Body size hint.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BodySize { pub enum BodySize {
/// Absence of body can be assumed from method or status code. /// Implicitly empty body.
/// ///
/// Will skip writing Content-Length header. /// Will omit the Content-Length header. Used for responses to certain methods (e.g., `HEAD`) or
/// with particular status codes (e.g., 204 No Content). Consumers that read this as a body size
/// hint are allowed to make optimizations that skip reading or writing the payload.
None, None,
/// Known size body. /// Known size body.
@ -18,6 +20,9 @@ pub enum BodySize {
} }
impl BodySize { impl BodySize {
/// Equivalent to `BodySize::Sized(0)`;
pub const ZERO: Self = Self::Sized(0);
/// Returns true if size hint indicates omitted or empty body. /// Returns true if size hint indicates omitted or empty body.
/// ///
/// Streams will return false because it cannot be known without reading the stream. /// Streams will return false because it cannot be known without reading the stream.

View file

@ -27,6 +27,7 @@ where
S: Stream<Item = Result<Bytes, E>>, S: Stream<Item = Result<Bytes, E>>,
E: Into<Box<dyn StdError>> + 'static, E: Into<Box<dyn StdError>> + 'static,
{ {
#[inline]
pub fn new(size: u64, stream: S) -> Self { pub fn new(size: u64, stream: S) -> Self {
SizedStream { size, stream } SizedStream { size, stream }
} }
@ -41,6 +42,7 @@ where
{ {
type Error = E; type Error = E;
#[inline]
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.size as u64) BodySize::Sized(self.size as u64)
} }

View file

@ -1,4 +1,5 @@
//! Error and Result module //! Error and Result module
// This is meant to be a glob import of the whole error module except for `Error`. Rustdoc can't yet // This is meant to be a glob import of the whole error module except for `Error`. Rustdoc can't yet
// correctly resolve the conflicting `Error` type defined in this module, so these re-exports are // correctly resolve the conflicting `Error` type defined in this module, so these re-exports are
// expanded manually. // expanded manually.