-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
C-bugCategory: This is a bug.Category: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Description
I expected that these invocations of sort_by
and sort_by_key
would be equivalent:
struct Client(String);
impl Client {
fn key(&self) -> &str {
&self.0
}
}
fn main() {
let mut clients: Vec<Client> = vec![];
// Error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
clients.sort_by_key(|c| c.key());
// OK
clients.sort_by(|a, b| a.key().cmp(&b.key()));
}
The implementation of sort_by_key
:
pub fn sort_by_key<B, F>(&mut self, mut f: F)
where F: FnMut(&T) -> B, B: Ord
{
self.sort_by(|a, b| f(a).cmp(&f(b)))
}
An initial attempt at using HRTB didn't seem to pan out:
pub fn sort_by_key<B, F>(&mut self, mut f: F)
where for <'a> F: FnMut(&'a T) -> B + 'a,
B: Ord
lilydjwg, LukasKalbertodt, bikeshedder, boardwalk, maur1th and 23 more
Metadata
Metadata
Assignees
Labels
C-bugCategory: This is a bug.Category: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.