|
| 1 | +#![deny( |
| 2 | + missing_debug_implementations, |
| 3 | + missing_docs, |
| 4 | + unreachable_pub, |
| 5 | + broken_intra_doc_links |
| 6 | +)] |
| 7 | +#![cfg_attr(test, deny(warnings))] |
| 8 | + |
| 9 | +//! Utilities for [`http_body::Body`]. |
| 10 | +//! |
| 11 | +//! [`BodyExt`] adds extensions to the common trait. |
| 12 | +//! |
| 13 | +//! [`Empty`] and [`Full`] provide simple implementations. |
| 14 | +
|
| 15 | +pub mod combinators; |
| 16 | +mod empty; |
| 17 | +mod full; |
| 18 | + |
| 19 | +pub use self::empty::Empty; |
| 20 | +pub use self::full::Full; |
| 21 | +use self::combinators::{BoxBody, MapData, MapErr, UnsyncBoxBody}; |
| 22 | + |
| 23 | +/// An extension trait for [`http_body::Body`] adding various combinators and adapters |
| 24 | +pub trait BodyExt: http_body::Body { |
| 25 | + /// Maps this body's data value to a different value. |
| 26 | + fn map_data<F, B>(self, f: F) -> MapData<Self, F> |
| 27 | + where |
| 28 | + Self: Sized, |
| 29 | + F: FnMut(Self::Data) -> B, |
| 30 | + B: bytes::Buf, |
| 31 | + { |
| 32 | + MapData::new(self, f) |
| 33 | + } |
| 34 | + |
| 35 | + /// Maps this body's error value to a different value. |
| 36 | + fn map_err<F, E>(self, f: F) -> MapErr<Self, F> |
| 37 | + where |
| 38 | + Self: Sized, |
| 39 | + F: FnMut(Self::Error) -> E, |
| 40 | + { |
| 41 | + MapErr::new(self, f) |
| 42 | + } |
| 43 | + |
| 44 | + /// Turn this body into a boxed trait object. |
| 45 | + fn boxed(self) -> BoxBody<Self::Data, Self::Error> |
| 46 | + where |
| 47 | + Self: Sized + Send + Sync + 'static, |
| 48 | + { |
| 49 | + BoxBody::new(self) |
| 50 | + } |
| 51 | + |
| 52 | + /// Turn this body into a boxed trait object that is !Sync. |
| 53 | + fn boxed_unsync(self) -> UnsyncBoxBody<Self::Data, Self::Error> |
| 54 | + where |
| 55 | + Self: Sized + Send + 'static, |
| 56 | + { |
| 57 | + UnsyncBoxBody::new(self) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl<T: ?Sized> BodyExt for T where T: http_body::Body {} |
0 commit comments