Skip to content

Commit 272f74c

Browse files
montekkiyoshuawuyts
authored andcommitted
fixes to stream::min_by (#162)
* fixes to stream::min_by * no reason to split these impls * remove Debug derive from MinByFuture
1 parent a8090be commit 272f74c

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/stream/stream/min_by.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ use std::cmp::Ordering;
22
use std::pin::Pin;
33

44
use crate::future::Future;
5-
use crate::stream::Stream;
65
use crate::task::{Context, Poll};
76

8-
/// A future that yields the minimum item in a stream by a given comparison function.
9-
#[derive(Clone, Debug)]
10-
pub struct MinByFuture<S: Stream, F> {
7+
#[allow(missing_debug_implementations)]
8+
pub struct MinByFuture<S, F, T> {
119
stream: S,
1210
compare: F,
13-
min: Option<S::Item>,
11+
min: Option<T>,
1412
}
1513

16-
impl<S: Stream + Unpin, F> Unpin for MinByFuture<S, F> {}
14+
impl<S, F, T> MinByFuture<S, F, T> {
15+
pin_utils::unsafe_pinned!(stream: S);
16+
pin_utils::unsafe_unpinned!(compare: F);
17+
pin_utils::unsafe_unpinned!(min: Option<T>);
1718

18-
impl<S: Stream + Unpin, F> MinByFuture<S, F> {
1919
pub(super) fn new(stream: S, compare: F) -> Self {
2020
MinByFuture {
2121
stream,
@@ -25,25 +25,25 @@ impl<S: Stream + Unpin, F> MinByFuture<S, F> {
2525
}
2626
}
2727

28-
impl<S, F> Future for MinByFuture<S, F>
28+
impl<S, F> Future for MinByFuture<S, F, S::Item>
2929
where
30-
S: futures_core::stream::Stream + Unpin,
30+
S: futures_core::stream::Stream + Unpin + Sized,
3131
S::Item: Copy,
3232
F: FnMut(&S::Item, &S::Item) -> Ordering,
3333
{
3434
type Output = Option<S::Item>;
3535

3636
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
37-
let next = futures_core::ready!(Pin::new(&mut self.stream).poll_next(cx));
37+
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
3838

3939
match next {
4040
Some(new) => {
4141
cx.waker().wake_by_ref();
42-
match self.as_mut().min.take() {
43-
None => self.as_mut().min = Some(new),
44-
Some(old) => match (&mut self.as_mut().compare)(&new, &old) {
45-
Ordering::Less => self.as_mut().min = Some(new),
46-
_ => self.as_mut().min = Some(old),
42+
match self.as_mut().min().take() {
43+
None => *self.as_mut().min() = Some(new),
44+
Some(old) => match (&mut self.as_mut().compare())(&new, &old) {
45+
Ordering::Less => *self.as_mut().min() = Some(new),
46+
_ => *self.as_mut().min() = Some(old),
4747
},
4848
}
4949
Poll::Pending

src/stream/stream/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ pub trait Stream {
153153
/// #
154154
/// # }) }
155155
/// ```
156-
fn min_by<F>(self, compare: F) -> MinByFuture<Self, F>
156+
fn min_by<F>(self, compare: F) -> MinByFuture<Self, F, Self::Item>
157157
where
158-
Self: Sized + Unpin,
158+
Self: Sized,
159159
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
160160
{
161161
MinByFuture::new(self, compare)

0 commit comments

Comments
 (0)