forked from mirror/async-std
[Draft PR] Adds Stream::gt (#304)
* [Draft PR] Adds Stream::gt * Applies cargo format and fixes incorrect comment * cargo fmt * fixes rustdoc related issues
This commit is contained in:
parent
f0f279ec04
commit
5f7238eec6
2 changed files with 95 additions and 7 deletions
47
src/stream/stream/gt.rs
Normal file
47
src/stream/stream/gt.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 those of another.
|
||||
#[doc(hidden)]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct GtFuture<L: Stream, R: Stream> {
|
||||
partial_cmp: PartialCmpFuture<L, R>,
|
||||
}
|
||||
|
||||
impl<L: Stream, R: Stream> GtFuture<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 {
|
||||
GtFuture {
|
||||
partial_cmp: l.partial_cmp(r),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: Stream, R: Stream> Future for GtFuture<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) => Poll::Ready(true),
|
||||
_ => Poll::Ready(false),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -32,6 +32,7 @@ mod find_map;
|
|||
mod fold;
|
||||
mod for_each;
|
||||
mod fuse;
|
||||
mod gt;
|
||||
mod inspect;
|
||||
mod le;
|
||||
mod lt;
|
||||
|
@ -57,6 +58,7 @@ use find::FindFuture;
|
|||
use find_map::FindMapFuture;
|
||||
use fold::FoldFuture;
|
||||
use for_each::ForEachFuture;
|
||||
use gt::GtFuture;
|
||||
use le::LeFuture;
|
||||
use lt::LtFuture;
|
||||
use min_by::MinByFuture;
|
||||
|
@ -1232,12 +1234,14 @@ extension_trait! {
|
|||
of another.
|
||||
|
||||
# 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]);
|
||||
|
@ -1263,11 +1267,48 @@ extension_trait! {
|
|||
PartialCmpFuture::new(self, other)
|
||||
}
|
||||
|
||||
#[doc = r#"
|
||||
Determines if the elements of this `Stream` are lexicographically
|
||||
greater than those of another.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
# fn main() { async_std::task::block_on(async {
|
||||
#
|
||||
use async_std::prelude::*;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
let single = VecDeque::from(vec![1]);
|
||||
let single_gt = VecDeque::from(vec![10]);
|
||||
let multi = VecDeque::from(vec![1,2]);
|
||||
let multi_gt = VecDeque::from(vec![1,5]);
|
||||
assert_eq!(single.clone().gt(single.clone()).await, false);
|
||||
assert_eq!(single_gt.clone().gt(single.clone()).await, true);
|
||||
assert_eq!(multi.clone().gt(single_gt.clone()).await, false);
|
||||
assert_eq!(multi_gt.clone().gt(multi.clone()).await, true);
|
||||
#
|
||||
# }) }
|
||||
```
|
||||
"#]
|
||||
fn gt<S>(
|
||||
self,
|
||||
other: S
|
||||
) -> impl Future<Output = bool> [GtFuture<Self, S>]
|
||||
where
|
||||
Self: Sized + Stream,
|
||||
S: Stream,
|
||||
<Self as Stream>::Item: PartialOrd<S::Item>,
|
||||
{
|
||||
GtFuture::new(self, other)
|
||||
}
|
||||
|
||||
#[doc = r#"
|
||||
Determines if the elements of this `Stream` are lexicographically
|
||||
less or equal to those of another.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
# fn main() { async_std::task::block_on(async {
|
||||
#
|
||||
|
@ -1303,6 +1344,7 @@ extension_trait! {
|
|||
less than those of another.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
# fn main() { async_std::task::block_on(async {
|
||||
#
|
||||
|
@ -1318,7 +1360,6 @@ extension_trait! {
|
|||
assert_eq!(single.clone().lt(single_gt.clone()).await, true);
|
||||
assert_eq!(multi.clone().lt(single_gt.clone()).await, true);
|
||||
assert_eq!(multi_gt.clone().lt(multi.clone()).await, false);
|
||||
|
||||
#
|
||||
# }) }
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue