forked from mirror/async-std
Adds Stream:ge (#285)
* Adds partial_cmp.rs file and partial_cmp signature to mod.rs * adds tests that compare streams of same length * Adds Stream::ge * cargo fmt * fixes rustdoc error
This commit is contained in:
parent
5f7238eec6
commit
a7041be6f2
2 changed files with 87 additions and 0 deletions
47
src/stream/stream/ge.rs
Normal file
47
src/stream/stream/ge.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
use std::cmp::Ordering;
|
||||
use std::pin::Pin;
|
||||
|
||||
use super::partial_cmp::PartialCmpFuture;
|
||||
use crate::future::Future;
|
||||
use crate::prelude::*;
|
||||
use crate::stream::Stream;
|
||||
use crate::task::{Context, Poll};
|
||||
|
||||
// Determines if the elements of this `Stream` are lexicographically
|
||||
// greater than or equal to those of another.
|
||||
#[doc(hidden)]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct GeFuture<L: Stream, R: Stream> {
|
||||
partial_cmp: PartialCmpFuture<L, R>,
|
||||
}
|
||||
|
||||
impl<L: Stream, R: Stream> GeFuture<L, R>
|
||||
where
|
||||
L::Item: PartialOrd<R::Item>,
|
||||
{
|
||||
pin_utils::unsafe_pinned!(partial_cmp: PartialCmpFuture<L, R>);
|
||||
|
||||
pub(super) fn new(l: L, r: R) -> Self {
|
||||
GeFuture {
|
||||
partial_cmp: l.partial_cmp(r),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: Stream, R: Stream> Future for GeFuture<L, R>
|
||||
where
|
||||
L: Stream + Sized,
|
||||
R: Stream + Sized,
|
||||
L::Item: PartialOrd<R::Item>,
|
||||
{
|
||||
type Output = bool;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let result = futures_core::ready!(self.as_mut().partial_cmp().poll(cx));
|
||||
|
||||
match result {
|
||||
Some(Ordering::Greater) | Some(Ordering::Equal) => Poll::Ready(true),
|
||||
_ => Poll::Ready(false),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -32,6 +32,7 @@ mod find_map;
|
|||
mod fold;
|
||||
mod for_each;
|
||||
mod fuse;
|
||||
mod ge;
|
||||
mod gt;
|
||||
mod inspect;
|
||||
mod le;
|
||||
|
@ -58,6 +59,7 @@ use find::FindFuture;
|
|||
use find_map::FindMapFuture;
|
||||
use fold::FoldFuture;
|
||||
use for_each::ForEachFuture;
|
||||
use ge::GeFuture;
|
||||
use gt::GtFuture;
|
||||
use le::LeFuture;
|
||||
use lt::LtFuture;
|
||||
|
@ -1240,6 +1242,7 @@ extension_trait! {
|
|||
#
|
||||
use async_std::prelude::*;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
let s1 = VecDeque::from(vec![1]);
|
||||
|
@ -1267,6 +1270,43 @@ extension_trait! {
|
|||
PartialCmpFuture::new(self, other)
|
||||
}
|
||||
|
||||
|
||||
#[doc = r#"
|
||||
Determines if the elements of this `Stream` are lexicographically
|
||||
greater than or equal to those of another.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
# fn main() { async_std::task::block_on(async {
|
||||
#
|
||||
use async_std::prelude::*;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
let single: VecDeque<isize> = vec![1].into_iter().collect();
|
||||
let single_gt: VecDeque<isize> = vec![10].into_iter().collect();
|
||||
let multi: VecDeque<isize> = vec![1,2].into_iter().collect();
|
||||
let multi_gt: VecDeque<isize> = vec![1,5].into_iter().collect();
|
||||
assert_eq!(single.clone().ge(single.clone()).await, true);
|
||||
assert_eq!(single_gt.clone().ge(single.clone()).await, true);
|
||||
assert_eq!(multi.clone().ge(single_gt.clone()).await, false);
|
||||
assert_eq!(multi_gt.clone().ge(multi.clone()).await, true);
|
||||
#
|
||||
# }) }
|
||||
```
|
||||
"#]
|
||||
fn ge<S>(
|
||||
self,
|
||||
other: S
|
||||
) -> impl Future<Output = bool> [GeFuture<Self, S>]
|
||||
where
|
||||
Self: Sized + Stream,
|
||||
S: Stream,
|
||||
<Self as Stream>::Item: PartialOrd<S::Item>,
|
||||
{
|
||||
GeFuture::new(self, other)
|
||||
}
|
||||
|
||||
#[doc = r#"
|
||||
Determines if the elements of this `Stream` are lexicographically
|
||||
greater than those of another.
|
||||
|
|
Loading…
Reference in a new issue