Skip to content

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

Open
@ramosbugs

Description

@ramosbugs

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.

Activity

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[/+] on Feb 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ramosbugs

        Issue actions

          StreamExt::max_by_key expects key to have same type as stream item · Issue #956 · async-rs/async-std