New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Stream::size_hint #1853
Merged
Merged
Add Stream::size_hint #1853
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,37 @@ pub trait Stream { | |
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
) -> Poll<Option<Self::Item>>; | ||
|
||
/// Returns the bounds on the remaining length of the stream. | ||
/// | ||
/// Specifically, `size_hint()` returns a tuple where the first element | ||
/// is the lower bound, and the second element is the upper bound. | ||
/// | ||
/// The second half of the tuple that is returned is an [`Option`]`<`[`usize`]`>`. | ||
/// A [`None`] here means that either there is no known upper bound, or the | ||
/// upper bound is larger than [`usize`]. | ||
/// | ||
/// # Implementation notes | ||
/// | ||
/// It is not enforced that a stream implementation yields the declared | ||
/// number of elements. A buggy stream may yield less than the lower bound | ||
/// or more than the upper bound of elements. | ||
/// | ||
/// `size_hint()` is primarily intended to be used for optimizations such as | ||
/// reserving space for the elements of the stream, but must not be | ||
/// trusted to e.g., omit bounds checks in unsafe code. An incorrect | ||
/// implementation of `size_hint()` should not lead to memory safety | ||
/// violations. | ||
/// | ||
/// That said, the implementation should provide a correct estimation, | ||
/// because otherwise it would be a violation of the trait's protocol. | ||
/// | ||
/// The default implementation returns `(0, `[`None`]`)` which is correct for any | ||
/// stream. | ||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(0, None) | ||
} | ||
} | ||
|
||
impl<S: ?Sized + Stream + Unpin> Stream for &mut S { | ||
|
@@ -70,6 +101,10 @@ impl<S: ?Sized + Stream + Unpin> Stream for &mut S { | |
) -> Poll<Option<Self::Item>> { | ||
S::poll_next(Pin::new(&mut **self), cx) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(**self).size_hint() | ||
} | ||
} | ||
|
||
impl<P> Stream for Pin<P> | ||
|
@@ -85,6 +120,10 @@ where | |
) -> Poll<Option<Self::Item>> { | ||
self.get_mut().as_mut().poll_next(cx) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(**self).size_hint() | ||
} | ||
} | ||
|
||
/// A stream which tracks whether or not the underlying stream | ||
|
@@ -126,7 +165,7 @@ mod private_try_stream { | |
|
||
/// A convenience for streams that return `Result` values that includes | ||
/// a variety of adapters tailored to such futures. | ||
pub trait TryStream: private_try_stream::Sealed { | ||
pub trait TryStream: Stream + private_try_stream::Sealed { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this bound because it would be convenient if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh...
|
||
/// The type of successful values yielded by this future | ||
type Ok; | ||
|
||
|
@@ -169,10 +208,30 @@ mod if_alloc { | |
) -> Poll<Option<Self::Item>> { | ||
Pin::new(&mut **self).poll_next(cx) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(**self).size_hint() | ||
} | ||
} | ||
|
||
impl<T: Unpin> Stream for alloc::collections::VecDeque<T> { | ||
type Item = T; | ||
|
||
fn poll_next( | ||
mut self: Pin<&mut Self>, | ||
_cx: &mut Context<'_>, | ||
) -> Poll<Option<Self::Item>> { | ||
Poll::Ready(self.pop_front()) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let len = self.len(); | ||
(len, Some(len)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl<S: Stream> Stream for ::std::panic::AssertUnwindSafe<S> { | ||
impl<S: Stream> Stream for std::panic::AssertUnwindSafe<S> { | ||
type Item = S::Item; | ||
|
||
fn poll_next( | ||
|
@@ -181,16 +240,9 @@ mod if_alloc { | |
) -> Poll<Option<S::Item>> { | ||
unsafe { self.map_unchecked_mut(|x| &mut x.0) }.poll_next(cx) | ||
} | ||
} | ||
|
||
impl<T: Unpin> Stream for ::alloc::collections::VecDeque<T> { | ||
type Item = T; | ||
|
||
fn poll_next( | ||
mut self: Pin<&mut Self>, | ||
_cx: &mut Context<'_>, | ||
) -> Poll<Option<Self::Item>> { | ||
Poll::Ready(self.pop_front()) | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
self.0.size_hint() | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added for consistency with
TryStream
.