@@ -59,6 +59,37 @@ pub trait Stream {
59
59
self : Pin < & mut Self > ,
60
60
cx : & mut Context < ' _ > ,
61
61
) -> 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
+ }
62
93
}
63
94
64
95
impl < S : ?Sized + Stream + Unpin > Stream for & mut S {
@@ -70,6 +101,10 @@ impl<S: ?Sized + Stream + Unpin> Stream for &mut S {
70
101
) -> Poll < Option < Self :: Item > > {
71
102
S :: poll_next ( Pin :: new ( & mut * * self ) , cx)
72
103
}
104
+
105
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
106
+ ( * * self ) . size_hint ( )
107
+ }
73
108
}
74
109
75
110
impl < P > Stream for Pin < P >
@@ -85,6 +120,10 @@ where
85
120
) -> Poll < Option < Self :: Item > > {
86
121
self . get_mut ( ) . as_mut ( ) . poll_next ( cx)
87
122
}
123
+
124
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
125
+ ( * * self ) . size_hint ( )
126
+ }
88
127
}
89
128
90
129
/// A stream which tracks whether or not the underlying stream
@@ -126,7 +165,7 @@ mod private_try_stream {
126
165
127
166
/// A convenience for streams that return `Result` values that includes
128
167
/// 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 {
130
169
/// The type of successful values yielded by this future
131
170
type Ok ;
132
171
@@ -169,10 +208,30 @@ mod if_alloc {
169
208
) -> Poll < Option < Self :: Item > > {
170
209
Pin :: new ( & mut * * self ) . poll_next ( cx)
171
210
}
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
+ }
172
231
}
173
232
174
233
#[ cfg( feature = "std" ) ]
175
- impl < S : Stream > Stream for :: std:: panic:: AssertUnwindSafe < S > {
234
+ impl < S : Stream > Stream for std:: panic:: AssertUnwindSafe < S > {
176
235
type Item = S :: Item ;
177
236
178
237
fn poll_next (
@@ -181,16 +240,9 @@ mod if_alloc {
181
240
) -> Poll < Option < S :: Item > > {
182
241
unsafe { self . map_unchecked_mut ( |x| & mut x. 0 ) } . poll_next ( cx)
183
242
}
184
- }
185
-
186
- impl < T : Unpin > Stream for :: alloc:: collections:: VecDeque < T > {
187
- type Item = T ;
188
243
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 ( )
194
246
}
195
247
}
196
248
0 commit comments