Skip to content

Commit f74ab33

Browse files
implement Body::size_hint
Co-authored-by: neoeinstein <[email protected]>
1 parent 60419ac commit f74ab33

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/limited.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Body types.
22
3-
use crate::Body;
3+
use crate::{Body, SizeHint};
44
use bytes::Buf;
55
use pin_project_lite::pin_project;
66
use std::{
@@ -72,6 +72,24 @@ where
7272
fn is_end_stream(&self) -> bool {
7373
self.inner.is_end_stream()
7474
}
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+
}
7593
}
7694

7795
#[cfg(test)]

0 commit comments

Comments
 (0)