Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove B type param: Follow ups #1789

Merged
merged 36 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4596c3c
Start working on v0.7.0
davidpdrsn Nov 25, 2022
8685796
Add `axum_core::body::Body` (#1584)
davidpdrsn Nov 27, 2022
97d3258
FromRequest
davidpdrsn Feb 12, 2023
b7f0dcd
Handler
davidpdrsn Feb 12, 2023
3b64f85
Router
davidpdrsn Feb 12, 2023
beeb567
MethodRouter
davidpdrsn Feb 12, 2023
a95da74
Remove `B` type param: axum-core (#1772)
davidpdrsn Feb 20, 2023
dea73f8
Remove `B` type param: axum-extra (#1775)
davidpdrsn Feb 21, 2023
69e8b04
Remove `B` type param: `Router`, `MethodRouter`, `Handler` (#1774)
davidpdrsn Feb 21, 2023
92722fc
Remove `B` type param: rest of axum (#1776)
davidpdrsn Feb 21, 2023
b9e0c0c
Remove `B` type param: the rest (#1778)
davidpdrsn Feb 28, 2023
372f023
Remove `B` from everything else
davidpdrsn Feb 12, 2023
00c0e88
update UI tests
davidpdrsn Feb 21, 2023
4e6b7c3
add Request<Body> type alias
davidpdrsn Feb 25, 2023
f7898ba
use Request type alias
davidpdrsn Feb 25, 2023
e89f303
impl FromRequest for Body, remove RawBody
davidpdrsn Feb 25, 2023
500aa5b
Remove `BoxBody` and `boxed`, just use `Body`
davidpdrsn Feb 25, 2023
6f371aa
Update examples
davidpdrsn Feb 25, 2023
685c304
The request body is already a `Body`
davidpdrsn Feb 25, 2023
298ef4b
format
davidpdrsn Feb 25, 2023
4f29ed7
remove mention of `BoxBody`
davidpdrsn Feb 25, 2023
f371430
fix ui tests
davidpdrsn Feb 28, 2023
9c2c0fd
Start working on v0.7.0
davidpdrsn Nov 25, 2022
29d03be
Add `axum_core::body::Body` (#1584)
davidpdrsn Nov 27, 2022
daff377
Change `sse::Event::json_data` to use `axum_core::Error` as its error…
davidpdrsn Feb 16, 2023
d87dac6
Fix typo in extract::ws (#1664)
mscofield0 Feb 24, 2023
c8cd23f
Remove `B` type param (#1751)
davidpdrsn Mar 12, 2023
63a947a
Merge branch 'v0.7.0' into remove-b-type-param-follow-ups
davidpdrsn Mar 12, 2023
8166f52
fix ui tests
davidpdrsn Mar 12, 2023
8693126
Remove re-exports of `Empty` and `Full`
davidpdrsn Mar 12, 2023
0bd961a
more changelog
davidpdrsn Mar 12, 2023
39910f1
fix doc tests
davidpdrsn Mar 12, 2023
7dc151a
how did the spa file get back?
davidpdrsn Mar 17, 2023
dda8349
fix changelog
davidpdrsn Mar 17, 2023
6c8144f
revert testing example changes
davidpdrsn Mar 17, 2023
4038a88
Merge branch 'v0.7.0' into remove-b-type-param-follow-ups
davidpdrsn Mar 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 94 additions & 9 deletions axum-core/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
use crate::{BoxError, Error};
use bytes::Bytes;
use bytes::{Buf, BufMut};
use http_body::Body;
use futures_util::stream::Stream;
use http::HeaderMap;
use http_body::Body as _;
use std::pin::Pin;
use std::task::{Context, Poll};

/// A boxed [`Body`] trait object.
///
/// This is used in axum as the response body type for applications. It's
/// necessary to unify multiple response bodies types into one.
pub type BoxBody = http_body::combinators::UnsyncBoxBody<Bytes, Error>;
type BoxBody = http_body::combinators::UnsyncBoxBody<Bytes, Error>;

/// Convert a [`http_body::Body`] into a [`BoxBody`].
pub fn boxed<B>(body: B) -> BoxBody
fn boxed<B>(body: B) -> BoxBody
where
B: http_body::Body<Data = Bytes> + Send + 'static,
B::Error: Into<BoxError>,
Expand Down Expand Up @@ -55,7 +54,7 @@ where
// THE SOFTWARE.
pub(crate) async fn to_bytes<T>(body: T) -> Result<Bytes, T::Error>
where
T: Body,
T: http_body::Body,
{
futures_util::pin_mut!(body);

Expand Down Expand Up @@ -85,6 +84,92 @@ where
Ok(vec.into())
}

/// The body type used in axum requests and responses.
#[derive(Debug)]
pub struct Body(BoxBody);

impl Body {
/// Create a new `Body` that wraps another [`http_body::Body`].
pub fn new<B>(body: B) -> Self
where
B: http_body::Body<Data = Bytes> + Send + 'static,
B::Error: Into<BoxError>,
{
try_downcast(body).unwrap_or_else(|body| Self(boxed(body)))
}

/// Create an empty body.
pub fn empty() -> Self {
Self::new(http_body::Empty::new())
}
}

impl Default for Body {
fn default() -> Self {
Self::empty()
}
}

macro_rules! body_from_impl {
($ty:ty) => {
impl From<$ty> for Body {
fn from(buf: $ty) -> Self {
Self::new(http_body::Full::from(buf))
}
}
};
}

body_from_impl!(&'static [u8]);
body_from_impl!(std::borrow::Cow<'static, [u8]>);
body_from_impl!(Vec<u8>);

body_from_impl!(&'static str);
body_from_impl!(std::borrow::Cow<'static, str>);
body_from_impl!(String);

body_from_impl!(Bytes);

impl http_body::Body for Body {
type Data = Bytes;
type Error = Error;

#[inline]
fn poll_data(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> std::task::Poll<Option<Result<Self::Data, Self::Error>>> {
Pin::new(&mut self.0).poll_data(cx)
}

#[inline]
fn poll_trailers(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> std::task::Poll<Result<Option<HeaderMap>, Self::Error>> {
Pin::new(&mut self.0).poll_trailers(cx)
}

#[inline]
fn size_hint(&self) -> http_body::SizeHint {
self.0.size_hint()
}

#[inline]
fn is_end_stream(&self) -> bool {
self.0.is_end_stream()
}
}

impl Stream for Body {
type Item = Result<Bytes, Error>;

#[inline]
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.poll_data(cx)
}
}

#[test]
fn test_try_downcast() {
assert_eq!(try_downcast::<i32, _>(5_u32), Err(5_u32));
Expand Down
Loading