mark some messagebody impls as infallible

This commit is contained in:
Rob Ede 2021-04-22 17:54:46 +01:00
parent 2a5c7bc3c1
commit 375a067495
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 19 additions and 26 deletions

View File

@ -184,6 +184,7 @@ where
pub struct BoxAnyBody(Pin<Box<dyn MessageBody<Error = Box<dyn StdError + 'static>>>>); pub struct BoxAnyBody(Pin<Box<dyn MessageBody<Error = Box<dyn StdError + 'static>>>>);
impl BoxAnyBody { impl BoxAnyBody {
/// Boxes a `MessageBody` and any errors it generates.
pub fn from_body<B>(body: B) -> Self pub fn from_body<B>(body: B) -> Self
where where
B: MessageBody + 'static, B: MessageBody + 'static,

View File

@ -1,6 +1,7 @@
//! [`MessageBody`] trait and foreign implementations. //! [`MessageBody`] trait and foreign implementations.
use std::{ use std::{
convert::Infallible,
mem, mem,
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
@ -29,7 +30,7 @@ pub trait MessageBody {
} }
impl MessageBody for () { impl MessageBody for () {
type Error = Error; type Error = Infallible;
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Empty BodySize::Empty
@ -43,12 +44,12 @@ impl MessageBody for () {
} }
} }
impl<T> MessageBody for Box<T> impl<B> MessageBody for Box<B>
where where
T: MessageBody + Unpin, B: MessageBody + Unpin,
T::Error: Into<Error>, B::Error: Into<Error>,
{ {
type Error = Error; type Error = B::Error;
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
self.as_ref().size() self.as_ref().size()
@ -58,20 +59,16 @@ where
self: Pin<&mut Self>, self: Pin<&mut Self>,
cx: &mut Context<'_>, cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Self::Error>>> { ) -> Poll<Option<Result<Bytes, Self::Error>>> {
match ready!(Pin::new(self.get_mut().as_mut()).poll_next(cx)) { Pin::new(self.get_mut().as_mut()).poll_next(cx)
Some(Err(err)) => Poll::Ready(Some(Err(err.into()))),
Some(Ok(val)) => Poll::Ready(Some(Ok(val))),
None => Poll::Ready(None),
}
} }
} }
impl<T> MessageBody for Pin<Box<T>> impl<B> MessageBody for Pin<Box<B>>
where where
T: MessageBody, B: MessageBody,
T::Error: Into<Error>, B::Error: Into<Error>,
{ {
type Error = Error; type Error = B::Error;
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
self.as_ref().size() self.as_ref().size()
@ -81,16 +78,12 @@ where
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,
cx: &mut Context<'_>, cx: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Self::Error>>> { ) -> Poll<Option<Result<Bytes, Self::Error>>> {
match ready!(self.as_mut().poll_next(cx)) { self.as_mut().poll_next(cx)
Some(Err(err)) => Poll::Ready(Some(Err(err))),
Some(Ok(val)) => Poll::Ready(Some(Ok(val))),
None => Poll::Ready(None),
}
} }
} }
impl MessageBody for Bytes { impl MessageBody for Bytes {
type Error = Error; type Error = Infallible;
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
@ -109,7 +102,7 @@ impl MessageBody for Bytes {
} }
impl MessageBody for BytesMut { impl MessageBody for BytesMut {
type Error = Error; type Error = Infallible;
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
@ -128,7 +121,7 @@ impl MessageBody for BytesMut {
} }
impl MessageBody for &'static str { impl MessageBody for &'static str {
type Error = Error; type Error = Infallible;
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
@ -149,7 +142,7 @@ impl MessageBody for &'static str {
} }
impl MessageBody for Vec<u8> { impl MessageBody for Vec<u8> {
type Error = Error; type Error = Infallible;
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)
@ -168,7 +161,7 @@ impl MessageBody for Vec<u8> {
} }
impl MessageBody for String { impl MessageBody for String {
type Error = Error; type Error = Infallible;
fn size(&self) -> BodySize { fn size(&self) -> BodySize {
BodySize::Sized(self.len() as u64) BodySize::Sized(self.len() as u64)

View File

@ -106,8 +106,7 @@ impl From<()> for Error {
impl From<std::convert::Infallible> for Error { impl From<std::convert::Infallible> for Error {
fn from(_: std::convert::Infallible) -> Self { fn from(_: std::convert::Infallible) -> Self {
// `std::convert::Infallible` indicates an error // hint that an error that will never happen
// that will never happen
unreachable!() unreachable!()
} }
} }