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

Array::sort_by_key doesn't work with Value::as_* methods #639

Closed
calavera opened this issue Oct 26, 2023 · 0 comments · Fixed by #642
Closed

Array::sort_by_key doesn't work with Value::as_* methods #639

calavera opened this issue Oct 26, 2023 · 0 comments · Fixed by #642

Comments

@calavera
Copy link

The new Array::sort_by_key doesn't seem to work when combined with the Value::as_* methods that the crate provides, like Value::as_str. Since I'm guessing that's the most common way to use that sorting function, I'd expect this test below to work, but it doesn't compile:

    #[test]
    fn sorting_with_references() {
        let values = vec!["foo", "qux", "bar"];
        let mut array = Array::from_iter(values);
        array.sort_by_key(Value::as_str);
    }

Compilation error:

error[E0308]: mismatched types
   --> crates/toml_edit/src/array.rs:433:9
    |
433 |         array.sort_by_key(Value::as_str);
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
    |
    = note: expected enum `Option<&str>`
               found enum `Option<&str>`
note: the lifetime requirement is introduced here
   --> crates/toml_edit/src/array.rs:327:29
    |
327 |         F: FnMut(&Value) -> K,
    |                             ^

For more information about this error, try `rustc --explain E0308`.
epage added a commit to epage/toml_edit that referenced this issue Oct 27, 2023
`Array::sort_by_key` can't really use a borrowed key because it will be
shuffling things around.  As an alternative, we can just provide the
`cmp` to use.

Fixes toml-rs#639
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

Successfully merging a pull request may close this issue.

1 participant