Skip to content
Myr

axum-core: fix body stream size hints (#3925)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Shuvro Roy · committed by GitHub · parent 9f1226d · Browse files · 2 files+34 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+34 added−12 removed

  • other:+34 added−12 removed

2 files+34 added−12 removed

modified:axum-core/src/body.rs+32 added-12 removed
182 unmodified lines
183 None => return Poll::Ready(None),184 }185 }186 }187188 #[inline]189 fn size_hint(&self) -> (usize, Option<usize>) {190 let size_hint = self.inner.size_hint();191 let lower = usize::try_from(size_hint.lower()).unwrap_or_default();192 let upper = size_hint.upper().and_then(|v| usize::try_from(v).ok());193 (lower, upper)194 }187}188189impl http_body::Body for BodyDataStream {190 type Data = Bytes;
46 unmodified lines
237 }238 }239}240241#[cfg(test)]242mod tests {243 use futures_core::Stream;244245 use crate::body::{try_downcast, Body};246249#[test]250fn test_try_downcast() {251 assert_eq!(try_downcast::<i32, _>(5_u32), Err(5_u32));252 assert_eq!(try_downcast::<i32, _>(5_i32), Ok(5_i32));247 #[test]248 fn test_try_downcast() {249 assert_eq!(try_downcast::<i32, _>(5_u32), Err(5_u32));250 assert_eq!(try_downcast::<i32, _>(5_i32), Ok(5_i32));251 }252253 #[test]254 fn data_stream_size_hints() {255 let stream = Body::empty().into_data_stream();256 assert_eq!(Stream::size_hint(&stream), (0, None));257 let body_hint = http_body::Body::size_hint(&stream);258 assert_eq!(body_hint.lower(), 0);259 assert_eq!(body_hint.upper(), Some(0));260261 let stream = Body::from("hello").into_data_stream();262 assert_eq!(Stream::size_hint(&stream), (0, None));263 let body_hint = http_body::Body::size_hint(&stream);264 assert_eq!(body_hint.lower(), 5);265 assert_eq!(body_hint.upper(), Some(5));266267 let stream = Body::from_stream(stream).into_data_stream();268 assert_eq!(Stream::size_hint(&stream), (0, None));269 let body_hint = http_body::Body::size_hint(&stream);270 assert_eq!(body_hint.lower(), 0);271 assert_eq!(body_hint.upper(), None);272 }273}
modified:axum-core/CHANGELOG.md+2 added
14 unmodified lines
15 `impl IntoResponse for HeaderMap`, `impl IntoResponse for Extensions` and16 others) now returns a body of unknown size.17- **fixed:** Make the `BytesMut` extractor ignore non-data frames from custom18 request bodies, matching the `Bytes` extractor ([#3811])19- **fixed:** `BodyDataStream` no longer reports invalid values in `Stream::size_hint` ([#3925])2021[#3721]: https://github.com/tokio-rs/axum/pull/372122[#3742]: https://github.com/tokio-rs/axum/pull/374223[#3811]: https://github.com/tokio-rs/axum/pull/381124[#3925]: https://github.com/tokio-rs/axum/pull/39252526# 0.5.62728Improve error messages with `#[diagnostic::do_not_recommend]`.
329 unmodified lines