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
Entry API #30
Comments
Hey @Ralith! I don't think I have a clear grasp of what your use case is. Would you mind providing an example? From what I do understand, it sounds like you could use an additional method like the
I don't like the name |
The use case, and desired semantics, are exactly that of the std |
Gotcha, definitely seems useful! I'll take a crack at it when I get some cycles, but it will probably be a larger change so I don't anticipate I'll be able to get to it in the near future. |
Actually, this is even more needed cause I can't get a value, return a reference to it or if missing create the value and return it cause it's borrow the cache as mutable more than one. It's quite annoying for a cache to not be able to do this basic operation. |
I think one question for this API will be at what point an occupied entry is considered "used" and moved to the back of the queue? I think there are broadly three options:
|
Would love to see this implemented! let value = match cache.get(&key) {
Some(value) => value,
None => {
let val = foo();
cache.put(key, val);
cache.get(key).expect("item should exist")
}
}; It would be much better to do: let value = cache.entry(key).or_insert_with(foo); |
Note that There's also no Finally, the entry API has other advantages, like working better when the function to create the missing value can fail. |
I also miss |
Another use-case for the entry API is referencing the key in the let k: String = "some-key".into();
let v = match map.entry(k) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(v) => {
v.insert(query_my_server_for_whatever(v.key().as_str())?)
}
}; |
A common operation for a cache is to search for an element and insert it if it's missing before returning a reference to the element. This shouldn't require hashing the key
twicethree times.The text was updated successfully, but these errors were encountered: