We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Body::size_hint
1 parent 60419ac commit f74ab33Copy full SHA for f74ab33
src/limited.rs
@@ -1,6 +1,6 @@
1
//! Body types.
2
3
-use crate::Body;
+use crate::{Body, SizeHint};
4
use bytes::Buf;
5
use pin_project_lite::pin_project;
6
use std::{
@@ -72,6 +72,24 @@ where
72
fn is_end_stream(&self) -> bool {
73
self.inner.is_end_stream()
74
}
75
+
76
+ fn size_hint(&self) -> SizeHint {
77
+ use std::convert::TryFrom;
78
+ match u64::try_from(self.limit) {
79
+ Ok(n) => {
80
+ let mut hint = self.inner.size_hint();
81
+ if hint.lower() >= n {
82
+ hint.set_exact(n)
83
+ } else if let Some(max) = hint.upper() {
84
+ hint.set_upper(n.min(max))
85
+ } else {
86
+ hint.set_upper(n)
87
+ }
88
+ hint
89
90
+ Err(_) => self.inner.size_hint(),
91
92
93
94
95
#[cfg(test)]
0 commit comments