-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Comments
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. |
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. |
Well, could we achieve this by passing a "conversion" closure instead of a "comparision" closure ? Something along the lines of :
That could be used like (Sorry I'm pretty sure I got the lifetimes and references wrong, but the idea is here). |
@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 |
@thestinger ok, I see the problem now. Indeed, the design of cc @vhbit and @alexcrichton, who worked on the original PR. Maybe they will have some ideas to alleviate the problem. |
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. |
Simplified example in #15749 |
I consider this fixed by the documentation update in #15749. |
…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.
As far as I understand, the merged PR #15220 introduces the methods
find_with
andfind_mut_with
onTreeMap
, 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 aTreeMap
usingString
keys to be searched without performing an allocation, usingt.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:
This prints
None
.The text was updated successfully, but these errors were encountered: