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

Generic key type prevents finding with non-static slices #8

Closed
chris-morgan opened this issue Jul 15, 2014 · 6 comments
Closed

Generic key type prevents finding with non-static slices #8

chris-morgan opened this issue Jul 15, 2014 · 6 comments

Comments

@chris-morgan
Copy link
Contributor

PhfMap<&'static str, Keyword> has K set to &'static str. This is an absolute killer for rust-phf, negating (without unsafe code) the whole point of the library.

fn find<'a>(&'a self, key: &K) -> Option<&'a V>

In other words, this is

fn find<'a>(&'a self, key: &&'static str) -> Option<&'a Keyword>

See the problem? I must pass the key as a reference to a static string.

If I wanted to do it based on user input—by far the most common case, I expect—I cannot do that, for it is not &'static str.

As it stands, I must cheat by transmuting the lifetime for the duration of the call, knowing it to be safe.

We evidently need something else. Straight generics don’t cut it.

@sfackler
Copy link
Collaborator

find_equiv exists to work around this problem.

@sfackler
Copy link
Collaborator

But I agree it is an issue.

@chris-morgan
Copy link
Contributor Author

I had assumed that find_equiv was going to be inefficient. Am I wrong in that assumption?

@sfackler
Copy link
Collaborator

find_equiv has exactly the same performance characteristics as find. It just changes the final equality check from a == b to a.equiv(b).

@chris-morgan
Copy link
Contributor Author

(I was unaware of the unwritten assumption that Equiv means the same hash also—this assumption is what allows it to be just as efficient, but it scares me significantly!)

@sfackler
Copy link
Collaborator

It is a bit dubious, but it's consistent with the way it's used in HashMap. I think @aturon is working on/thinking about a better solution.

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

2 participants