async-rs / async-rs/async-std

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

Open
#956 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.1k
Forks
339
PR merge metrics
No merged PRs in 30d

Description

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:
```rust
fn max_by_key(
self,
key_by: F,
) -> impl Future> [MaxByKeyFuture]
where
Self: Sized,
B: Ord,
F: FnMut(&Self::Item) -> B,
```

however, the underlying `MaxByKeyFuture` struct effectively requires `B` to match `Self::Item`:
```rust
impl Future for MaxByKeyFuture
where
S: Stream,
K: FnMut(&S::Item) -> S::Item,
S::Item: Ord,
{
type Output = Option;
...
}
```

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`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.