-
Notifications
You must be signed in to change notification settings - Fork 156
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
feature request: get_or_insert method #169
Comments
If let dashmap = DashMap::<String, i32>::new();
dashmap.insert("a".to_string(), 1);
{
let entry = dashmap.entry("a".to_string());
match entry {
Entry::Occupied(ref o) => println!("{}", o.get()),
Entry::Vacant(_) => panic!("should not go here"),
}
}
{
let entry = dashmap.entry("b".to_string());
match entry {
Entry::Occupied(_) => panic!("should not go here"),
Entry::Vacant(vacant) => {
vacant.insert(2);
}
}
}
println!("{}", dashmap.get("b").unwrap().value()); Both |
do you mean this is how it could be implemented? i don’t know how dashmap’s private code works but one of the contributors could probably come up with the best solution using methods that aren’t exposed publicly |
not exactly. I just show that your example code is not concurrent safe and if you had used it in your project to temporarily fill the lack of |
The entry API is stable and this is one of it's intended use cases. If this doesn't work for you and you have a proposition for a better API, please reopen. Closing as I don't want two API's that does the same thing. This is still a fairly niche use case which is why it's under the entry API. |
For those stumbling across this in future. See a possible solution here: |
a utility method to do this, possibly more optimized, would be nice since i think it's used very often
this way we could just do
let value = dashmap.get_or_insert(key(), default());
The text was updated successfully, but these errors were encountered: