Skip to content

Commit cc75b65

Browse files
authored
Merge pull request #409 from yjhmelody/stream-min
Add Stream min
2 parents f102588 + 021862d commit cc75b65

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/stream/stream/min.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::marker::PhantomData;
2+
use std::cmp::{Ordering, Ord};
3+
use std::pin::Pin;
4+
5+
use pin_project_lite::pin_project;
6+
7+
use crate::future::Future;
8+
use crate::stream::Stream;
9+
use crate::task::{Context, Poll};
10+
11+
pin_project! {
12+
#[doc(hidden)]
13+
#[allow(missing_debug_implementations)]
14+
pub struct MinFuture<S, F, T> {
15+
#[pin]
16+
stream: S,
17+
_compare: PhantomData<F>,
18+
min: Option<T>,
19+
}
20+
}
21+
22+
impl<S, F, T> MinFuture<S, F, T> {
23+
pub(super) fn new(stream: S) -> Self {
24+
Self {
25+
stream,
26+
_compare: PhantomData,
27+
min: None,
28+
}
29+
}
30+
}
31+
32+
impl<S, F> Future for MinFuture<S, F, S::Item>
33+
where
34+
S: Stream,
35+
S::Item: Ord,
36+
F: FnMut(&S::Item, &S::Item) -> Ordering,
37+
{
38+
type Output = Option<S::Item>;
39+
40+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
41+
let this = self.project();
42+
let next = futures_core::ready!(this.stream.poll_next(cx));
43+
44+
match next {
45+
Some(new) => {
46+
cx.waker().wake_by_ref();
47+
match this.min.take() {
48+
None => *this.min = Some(new),
49+
50+
Some(old) => match new.cmp(&old) {
51+
Ordering::Less => *this.min = Some(new),
52+
_ => *this.min = Some(old),
53+
},
54+
}
55+
Poll::Pending
56+
}
57+
None => Poll::Ready(this.min.take()),
58+
}
59+
}
60+
}

src/stream/stream/mod.rs

+36
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod le;
4242
mod lt;
4343
mod map;
4444
mod max_by;
45+
mod min;
4546
mod min_by;
4647
mod min_by_key;
4748
mod ne;
@@ -75,6 +76,7 @@ use last::LastFuture;
7576
use le::LeFuture;
7677
use lt::LtFuture;
7778
use max_by::MaxByFuture;
79+
use min::MinFuture;
7880
use min_by::MinByFuture;
7981
use min_by_key::MinByKeyFuture;
8082
use ne::NeFuture;
@@ -766,6 +768,40 @@ extension_trait! {
766768
MinByFuture::new(self, compare)
767769
}
768770

771+
#[doc = r#"
772+
Returns the element that gives the minimum value. If several elements are equally minimum,
773+
the first element is returned. If the stream is empty, `None` is returned.
774+
775+
# Examples
776+
777+
```
778+
# fn main() { async_std::task::block_on(async {
779+
#
780+
use std::collections::VecDeque;
781+
782+
use async_std::prelude::*;
783+
784+
let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
785+
786+
let min = s.clone().min().await;
787+
assert_eq!(min, Some(1));
788+
789+
let min = VecDeque::<usize>::new().min().await;
790+
assert_eq!(min, None);
791+
#
792+
# }) }
793+
```
794+
"#]
795+
fn min<F>(
796+
self,
797+
) -> impl Future<Output = Option<Self::Item>> [MinFuture<Self, F, Self::Item>]
798+
where
799+
Self: Sized,
800+
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
801+
{
802+
MinFuture::new(self)
803+
}
804+
769805
#[doc = r#"
770806
Returns the element that gives the maximum value with respect to the
771807
specified comparison function. If several elements are equally maximum,

0 commit comments

Comments
 (0)