mirror of
https://github.com/async-rs/async-std.git
synced 2025-02-28 07:09:40 +00:00
fixes to stream::min_by (#162)
* fixes to stream::min_by * no reason to split these impls * remove Debug derive from MinByFuture
This commit is contained in:
parent
a8090be3eb
commit
272f74c1da
2 changed files with 17 additions and 17 deletions
|
@ -2,20 +2,20 @@ use std::cmp::Ordering;
|
|||
use std::pin::Pin;
|
||||
|
||||
use crate::future::Future;
|
||||
use crate::stream::Stream;
|
||||
use crate::task::{Context, Poll};
|
||||
|
||||
/// A future that yields the minimum item in a stream by a given comparison function.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MinByFuture<S: Stream, F> {
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct MinByFuture<S, F, T> {
|
||||
stream: S,
|
||||
compare: F,
|
||||
min: Option<S::Item>,
|
||||
min: Option<T>,
|
||||
}
|
||||
|
||||
impl<S: Stream + Unpin, F> Unpin for MinByFuture<S, F> {}
|
||||
impl<S, F, T> MinByFuture<S, F, T> {
|
||||
pin_utils::unsafe_pinned!(stream: S);
|
||||
pin_utils::unsafe_unpinned!(compare: F);
|
||||
pin_utils::unsafe_unpinned!(min: Option<T>);
|
||||
|
||||
impl<S: Stream + Unpin, F> MinByFuture<S, F> {
|
||||
pub(super) fn new(stream: S, compare: F) -> Self {
|
||||
MinByFuture {
|
||||
stream,
|
||||
|
@ -25,25 +25,25 @@ impl<S: Stream + Unpin, F> MinByFuture<S, F> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S, F> Future for MinByFuture<S, F>
|
||||
impl<S, F> Future for MinByFuture<S, F, S::Item>
|
||||
where
|
||||
S: futures_core::stream::Stream + Unpin,
|
||||
S: futures_core::stream::Stream + Unpin + Sized,
|
||||
S::Item: Copy,
|
||||
F: FnMut(&S::Item, &S::Item) -> Ordering,
|
||||
{
|
||||
type Output = Option<S::Item>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let next = futures_core::ready!(Pin::new(&mut self.stream).poll_next(cx));
|
||||
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||
|
||||
match next {
|
||||
Some(new) => {
|
||||
cx.waker().wake_by_ref();
|
||||
match self.as_mut().min.take() {
|
||||
None => self.as_mut().min = Some(new),
|
||||
Some(old) => match (&mut self.as_mut().compare)(&new, &old) {
|
||||
Ordering::Less => self.as_mut().min = Some(new),
|
||||
_ => self.as_mut().min = Some(old),
|
||||
match self.as_mut().min().take() {
|
||||
None => *self.as_mut().min() = Some(new),
|
||||
Some(old) => match (&mut self.as_mut().compare())(&new, &old) {
|
||||
Ordering::Less => *self.as_mut().min() = Some(new),
|
||||
_ => *self.as_mut().min() = Some(old),
|
||||
},
|
||||
}
|
||||
Poll::Pending
|
||||
|
|
|
@ -153,9 +153,9 @@ pub trait Stream {
|
|||
/// #
|
||||
/// # }) }
|
||||
/// ```
|
||||
fn min_by<F>(self, compare: F) -> MinByFuture<Self, F>
|
||||
fn min_by<F>(self, compare: F) -> MinByFuture<Self, F, Self::Item>
|
||||
where
|
||||
Self: Sized + Unpin,
|
||||
Self: Sized,
|
||||
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
|
||||
{
|
||||
MinByFuture::new(self, compare)
|
||||
|
|
Loading…
Reference in a new issue