async-std icon indicating copy to clipboard operation
async-std copied to clipboard

StreamExt::max_by_key expects key to have same type as stream item

Open ramosbugs opened this issue 4 years ago • 0 comments

This is a bug report related to the ~~unstable~~ max_by_key feature tracked in #129. It may affect min_by_key and other related features as well, but I haven't investigated those.

The type signature of max_by_key itself appears correct:

fn max_by_key<B, F>(
    self,
    key_by: F,
) -> impl Future<Output = Option<Self::Item>> [MaxByKeyFuture<Self, Self::Item, F>]
where
    Self: Sized,
    B: Ord,
    F: FnMut(&Self::Item) -> B,

however, the underlying MaxByKeyFuture struct effectively requires B to match Self::Item:

impl<S, K> Future for MaxByKeyFuture<S, S::Item, K>
where
    S: Stream,
    K: FnMut(&S::Item) -> S::Item,
    S::Item: Ord,
{
    type Output = Option<S::Item>;
    ...
}

The issue is caused by the bound K: FnMut(&S::Item) -> S::Item. The return type of K should probably be a new generic type parameter B instead of Self::Item.

ramosbugs avatar Feb 16 '21 19:02 ramosbugs