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
Provenance
- Commit
a000c30- Tree
8c0ae9f- Author
Shuvro Roy <shuvro.nsu.cse@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
9f1226d
2 files changed+34 added−12 removed
modified:axum-core/src/body.rs+32 added-12 removed
182 unmodified lines183 None => return Poll::Ready(None),184 }185 }186 }187-188- #[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 }188 189 impl http_body::Body for BodyDataStream {190 type Data = Bytes;46 unmodified lines237 }238 }239 }240 241+#[cfg(test)]242+mod tests {243+ use futures_core::Stream;244+245+ use crate::body::{try_downcast, Body};246+249-#[test]250-fn 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+ }252+253+ #[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));260+261+ 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));266+267+ 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 lines15 `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])20 21 [#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/392525 26 # 0.5.627 28 Improve error messages with `#[diagnostic::do_not_recommend]`.329 unmodified lines