Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ramosbugs opened this issue Feb 16, 2021 · 0 comments
Open

Comments

@ramosbugs
Copy link

ramosbugs commented Feb 16, 2021

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 ramosbugs changed the title max_by_key expects key to have same type as stream item StreamExt::max_by_key expects key to have same type as stream item Feb 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant