-
Notifications
You must be signed in to change notification settings - Fork 341
Adds Stream::cmp #273
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
Merged
Merged
Adds Stream::cmp #273
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
86d45ee
Adds cmp
assemblaj 2f38dc2
Fixes formatting
assemblaj ae5a3e0
cleans up examples
assemblaj 507307f
attempts to fix rustdoc issue
assemblaj 42a8a2e
formats with cargo fmt
assemblaj cc049a0
Adds proper trait bounds for cmp
assemblaj 1e5a0bb
Merge branch 'master' into assemblaj-cmp
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use std::cmp::Ordering; | ||
use std::pin::Pin; | ||
|
||
use super::fuse::Fuse; | ||
use crate::future::Future; | ||
use crate::prelude::*; | ||
use crate::stream::Stream; | ||
use crate::task::{Context, Poll}; | ||
|
||
// Lexicographically compares the elements of this `Stream` with those | ||
// of another using `Ord`. | ||
#[doc(hidden)] | ||
#[allow(missing_debug_implementations)] | ||
pub struct CmpFuture<L: Stream, R: Stream> { | ||
l: Fuse<L>, | ||
r: Fuse<R>, | ||
l_cache: Option<L::Item>, | ||
r_cache: Option<R::Item>, | ||
} | ||
|
||
impl<L: Stream, R: Stream> CmpFuture<L, R> { | ||
pin_utils::unsafe_pinned!(l: Fuse<L>); | ||
pin_utils::unsafe_pinned!(r: Fuse<R>); | ||
pin_utils::unsafe_unpinned!(l_cache: Option<L::Item>); | ||
pin_utils::unsafe_unpinned!(r_cache: Option<R::Item>); | ||
|
||
pub(super) fn new(l: L, r: R) -> Self { | ||
CmpFuture { | ||
l: l.fuse(), | ||
r: r.fuse(), | ||
l_cache: None, | ||
r_cache: None, | ||
} | ||
} | ||
} | ||
|
||
impl<L: Stream, R: Stream> Future for CmpFuture<L, R> | ||
where | ||
L: Stream + Sized, | ||
R: Stream<Item = L::Item> + Sized, | ||
L::Item: Ord, | ||
{ | ||
type Output = Ordering; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
loop { | ||
// Stream that completes earliest can be considered Less, etc | ||
let l_complete = self.l.done && self.as_mut().l_cache.is_none(); | ||
let r_complete = self.r.done && self.as_mut().r_cache.is_none(); | ||
|
||
if l_complete && r_complete { | ||
return Poll::Ready(Ordering::Equal); | ||
} else if l_complete { | ||
return Poll::Ready(Ordering::Less); | ||
} else if r_complete { | ||
return Poll::Ready(Ordering::Greater); | ||
} | ||
|
||
// Get next value if possible and necesary | ||
if !self.l.done && self.as_mut().l_cache.is_none() { | ||
let l_next = futures_core::ready!(self.as_mut().l().poll_next(cx)); | ||
if let Some(item) = l_next { | ||
*self.as_mut().l_cache() = Some(item); | ||
} | ||
} | ||
|
||
if !self.r.done && self.as_mut().r_cache.is_none() { | ||
let r_next = futures_core::ready!(self.as_mut().r().poll_next(cx)); | ||
if let Some(item) = r_next { | ||
*self.as_mut().r_cache() = Some(item); | ||
} | ||
} | ||
|
||
// Compare if both values are available. | ||
if self.as_mut().l_cache.is_some() && self.as_mut().r_cache.is_some() { | ||
let l_value = self.as_mut().l_cache().take().unwrap(); | ||
let r_value = self.as_mut().r_cache().take().unwrap(); | ||
let result = l_value.cmp(&r_value); | ||
|
||
if let Ordering::Equal = result { | ||
// Reset cache to prepare for next comparison | ||
*self.as_mut().l_cache() = None; | ||
*self.as_mut().r_cache() = None; | ||
} else { | ||
// Return non equal value | ||
return Poll::Ready(result); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
mod all; | ||
mod any; | ||
mod chain; | ||
mod cmp; | ||
mod enumerate; | ||
mod filter; | ||
mod filter_map; | ||
|
@@ -53,6 +54,7 @@ mod zip; | |
|
||
use all::AllFuture; | ||
use any::AnyFuture; | ||
use cmp::CmpFuture; | ||
use enumerate::Enumerate; | ||
use filter_map::FilterMap; | ||
use find::FindFuture; | ||
|
@@ -1270,6 +1272,43 @@ extension_trait! { | |
PartialCmpFuture::new(self, other) | ||
} | ||
|
||
#[doc = r#" | ||
Lexicographically compares the elements of this `Stream` with those | ||
of another using 'Ord'. | ||
|
||
# Examples | ||
|
||
``` | ||
# fn main() { async_std::task::block_on(async { | ||
# | ||
use async_std::prelude::*; | ||
use std::collections::VecDeque; | ||
|
||
use std::cmp::Ordering; | ||
let s1 = VecDeque::from(vec![1]); | ||
let s2 = VecDeque::from(vec![1, 2]); | ||
let s3 = VecDeque::from(vec![1, 2, 3]); | ||
let s4 = VecDeque::from(vec![1, 2, 4]); | ||
assert_eq!(s1.clone().cmp(s1.clone()).await, Ordering::Equal); | ||
assert_eq!(s1.clone().cmp(s2.clone()).await, Ordering::Less); | ||
assert_eq!(s2.clone().cmp(s1.clone()).await, Ordering::Greater); | ||
assert_eq!(s3.clone().cmp(s4.clone()).await, Ordering::Less); | ||
assert_eq!(s4.clone().cmp(s3.clone()).await, Ordering::Greater); | ||
# | ||
# }) } | ||
``` | ||
"#] | ||
fn cmp<S>( | ||
self, | ||
other: S | ||
) -> impl Future<Output = Ordering> [CmpFuture<Self, S>] | ||
where | ||
Self: Sized + Stream, | ||
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. Is 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. When I remove
|
||
S: Stream, | ||
<Self as Stream>::Item: Ord | ||
{ | ||
CmpFuture::new(self, other) | ||
} | ||
|
||
#[doc = r#" | ||
Determines if the elements of this `Stream` are lexicographically | ||
|
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.
Are we missing
Self::Item: Ord
here? I seeIterator::cmp
has this bound.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.
This is correct, I'd erroneously removed this to pass rustdoc tests. Pushing a commit to fix this.