@@ -544,6 +544,19 @@ pub trait Read {
544
544
Initializer :: zeroing ( )
545
545
}
546
546
547
+ /// Return an estimate of how many bytes would be read from this source until EOF,
548
+ /// or zero if that is unknown.
549
+ ///
550
+ /// This is used by [`read_to_end`] and [`read_to_string`] to pre-allocate a memory buffer.
551
+ ///
552
+ /// [`read_to_end`]: #method.read_to_end
553
+ /// [`read_to_string`]: #method.read_to_string
554
+ #[ unstable( feature = "read_size_hint" , issue = /* FIXME */ "0" ) ]
555
+ #[ inline]
556
+ fn size_hint ( & self ) -> usize {
557
+ 0
558
+ }
559
+
547
560
/// Read all bytes until EOF in this source, placing them into `buf`.
548
561
///
549
562
/// All bytes read from this source will be appended to the specified buffer
@@ -1720,6 +1733,14 @@ impl<T: Read, U: Read> Read for Chain<T, U> {
1720
1733
self . second . read ( buf)
1721
1734
}
1722
1735
1736
+ fn size_hint ( & self ) -> usize {
1737
+ if self . done_first {
1738
+ self . second . size_hint ( )
1739
+ } else {
1740
+ self . first . size_hint ( ) . saturating_add ( self . second . size_hint ( ) )
1741
+ }
1742
+ }
1743
+
1723
1744
unsafe fn initializer ( & self ) -> Initializer {
1724
1745
let initializer = self . first . initializer ( ) ;
1725
1746
if initializer. should_initialize ( ) {
@@ -1918,6 +1939,14 @@ impl<T: Read> Read for Take<T> {
1918
1939
Ok ( n)
1919
1940
}
1920
1941
1942
+ fn size_hint ( & self ) -> usize {
1943
+ if self . limit == 0 {
1944
+ 0
1945
+ } else {
1946
+ cmp:: min ( self . limit , self . inner . size_hint ( ) as u64 ) as usize
1947
+ }
1948
+ }
1949
+
1921
1950
unsafe fn initializer ( & self ) -> Initializer {
1922
1951
self . inner . initializer ( )
1923
1952
}
0 commit comments