@@ -59,6 +59,37 @@ pub trait Stream {
5959 self : Pin < & mut Self > ,
6060 cx : & mut Context < ' _ > ,
6161 ) -> Poll < Option < Self :: Item > > ;
62+
63+ /// Returns the bounds on the remaining length of the stream.
64+ ///
65+ /// Specifically, `size_hint()` returns a tuple where the first element
66+ /// is the lower bound, and the second element is the upper bound.
67+ ///
68+ /// The second half of the tuple that is returned is an [`Option`]`<`[`usize`]`>`.
69+ /// A [`None`] here means that either there is no known upper bound, or the
70+ /// upper bound is larger than [`usize`].
71+ ///
72+ /// # Implementation notes
73+ ///
74+ /// It is not enforced that a stream implementation yields the declared
75+ /// number of elements. A buggy stream may yield less than the lower bound
76+ /// or more than the upper bound of elements.
77+ ///
78+ /// `size_hint()` is primarily intended to be used for optimizations such as
79+ /// reserving space for the elements of the stream, but must not be
80+ /// trusted to e.g., omit bounds checks in unsafe code. An incorrect
81+ /// implementation of `size_hint()` should not lead to memory safety
82+ /// violations.
83+ ///
84+ /// That said, the implementation should provide a correct estimation,
85+ /// because otherwise it would be a violation of the trait's protocol.
86+ ///
87+ /// The default implementation returns `(0, `[`None`]`)` which is correct for any
88+ /// stream.
89+ #[ inline]
90+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
91+ ( 0 , None )
92+ }
6293}
6394
6495impl < S : ?Sized + Stream + Unpin > Stream for & mut S {
@@ -70,6 +101,10 @@ impl<S: ?Sized + Stream + Unpin> Stream for &mut S {
70101 ) -> Poll < Option < Self :: Item > > {
71102 S :: poll_next ( Pin :: new ( & mut * * self ) , cx)
72103 }
104+
105+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
106+ ( * * self ) . size_hint ( )
107+ }
73108}
74109
75110impl < P > Stream for Pin < P >
@@ -85,6 +120,10 @@ where
85120 ) -> Poll < Option < Self :: Item > > {
86121 self . get_mut ( ) . as_mut ( ) . poll_next ( cx)
87122 }
123+
124+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
125+ ( * * self ) . size_hint ( )
126+ }
88127}
89128
90129/// A stream which tracks whether or not the underlying stream
@@ -126,7 +165,7 @@ mod private_try_stream {
126165
127166/// A convenience for streams that return `Result` values that includes
128167/// a variety of adapters tailored to such futures.
129- pub trait TryStream : private_try_stream:: Sealed {
168+ pub trait TryStream : Stream + private_try_stream:: Sealed {
130169 /// The type of successful values yielded by this future
131170 type Ok ;
132171
@@ -169,10 +208,30 @@ mod if_alloc {
169208 ) -> Poll < Option < Self :: Item > > {
170209 Pin :: new ( & mut * * self ) . poll_next ( cx)
171210 }
211+
212+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
213+ ( * * self ) . size_hint ( )
214+ }
215+ }
216+
217+ impl < T : Unpin > Stream for alloc:: collections:: VecDeque < T > {
218+ type Item = T ;
219+
220+ fn poll_next (
221+ mut self : Pin < & mut Self > ,
222+ _cx : & mut Context < ' _ > ,
223+ ) -> Poll < Option < Self :: Item > > {
224+ Poll :: Ready ( self . pop_front ( ) )
225+ }
226+
227+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
228+ let len = self . len ( ) ;
229+ ( len, Some ( len) )
230+ }
172231 }
173232
174233 #[ cfg( feature = "std" ) ]
175- impl < S : Stream > Stream for :: std:: panic:: AssertUnwindSafe < S > {
234+ impl < S : Stream > Stream for std:: panic:: AssertUnwindSafe < S > {
176235 type Item = S :: Item ;
177236
178237 fn poll_next (
@@ -181,16 +240,9 @@ mod if_alloc {
181240 ) -> Poll < Option < S :: Item > > {
182241 unsafe { self . map_unchecked_mut ( |x| & mut x. 0 ) } . poll_next ( cx)
183242 }
184- }
185-
186- impl < T : Unpin > Stream for :: alloc:: collections:: VecDeque < T > {
187- type Item = T ;
188243
189- fn poll_next (
190- mut self : Pin < & mut Self > ,
191- _cx : & mut Context < ' _ > ,
192- ) -> Poll < Option < Self :: Item > > {
193- Poll :: Ready ( self . pop_front ( ) )
244+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
245+ self . 0 . size_hint ( )
194246 }
195247 }
196248
0 commit comments