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

TreeMap: find_with is surprising #15635

Closed
olivren opened this issue Jul 12, 2014 · 8 comments
Closed

TreeMap: find_with is surprising #15635

olivren opened this issue Jul 12, 2014 · 8 comments

Comments

@olivren
Copy link
Contributor

olivren commented Jul 12, 2014

As far as I understand, the merged PR #15220 introduces the methods find_with and find_mut_with on TreeMap, in order to provide a general solution to the problem of finding an equivalent string key. In particular, these methods make it possible to search a TreeMap using String keys to be searched without performing an allocation, using t.find_with(|k| "test".cmp(&k.as_slice()).

I see an issue here : this method only works if the logic of the closure passed as an argument is exactly the same than the logic used to provide the natural ordering of the keys. Using a different logic in the comparision closure does not work as expected, and gives random result.

The documentation on these methods don't warn about this very strong restriction, and instead they present a wrong use of the API : using these methods to find a string by performing case insensitive comparisions. It happens that the provided example works as expected, but it is easy to modify this example such that the search does not find the expected result.

Here is a variation of the provided example:

extern crate collections;
fn main() {
    use std::ascii::StrAsciiExt;

    let mut t = collections::treemap::TreeMap::new();
    t.insert("a1", "a1");
    t.insert("A2", "a2");

    let ua_key = "a1";
    let ua = t.find_with(|&k| {
       ua_key.cmp(&k.to_ascii_lower().as_slice())
    });

    println!("{}", ua);
}

This prints None.

@olivren
Copy link
Contributor Author

olivren commented Jul 12, 2014

As the provided example was part of the PR, I think the author of this change didn't realize that such a strong restriction exists on this method. I don't think it's just a documentation issue.

My opinion is that these methods have too much potential for being misused, compared to their benefits (saving one allocation), and that we should remove them.

@thestinger
Copy link
Contributor

Avoiding the memory allocation is important and is a feature provided by C++. It could be done by adding yet another trait... but it won't work well with the current trait coherence rules.

@olivren
Copy link
Contributor Author

olivren commented Jul 12, 2014

Well, could we achieve this by passing a "conversion" closure instead of a "comparision" closure ?

Something along the lines of :

fn find_convert<'a, T>(&'a self, T: &key, f: |&K| -> &T) -> Option<&'a V>

That could be used like t.find_convert("test", |k| k.as_slice()).

(Sorry I'm pretty sure I got the lifetimes and references wrong, but the idea is here).

@thestinger
Copy link
Contributor

@olivier-renaud: No, that's not enough. You're only considering one special case. In C++, and with this method in Rust, you can use a rope type to perform a lookup in a tree storing String keys. The C++ way relies on operator< allowing any number of overloads.

@olivren
Copy link
Contributor Author

olivren commented Jul 16, 2014

@thestinger ok, I see the problem now. Indeed, the design of find_with seems reasonable given this use case.

cc @vhbit and @alexcrichton, who worked on the original PR. Maybe they will have some ideas to alleviate the problem.

@alexcrichton
Copy link
Member

I'm fine with updating the documentation, but an interface such as this is quite powerful and I don't think that we should consider removing it. No safe action can be taken to trigger memory unsafety, which is what really matters, and in general Rust can't do much in preventing logic errors outside of the typesystem.

@vhbit
Copy link
Contributor

vhbit commented Jul 17, 2014

Simplified example in #15749

@thestinger
Copy link
Contributor

I consider this fixed by the documentation update in #15749.

bors added a commit to rust-lang-ci/rust that referenced this issue Nov 13, 2023
…eykril

Do not resolve inlayHint.textEdit for VSCode client

Closes rust-lang/rust-analyzer#15604

VSCode behaves strangely, allowing to navigate into label location, but not allowing to apply hint's text edit, after hint is resolved. See microsoft/vscode#193124 for details.

For now, stub hint resolution for VSCode specifically.
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

4 participants