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

Change OccupiedEntry/RawOccupiedEntryMut lifetimes #593

Closed
wants to merge 2 commits into from

Conversation

ChosenName
Copy link

Changes the lifetimes returned by key(_mut), get(_mut) and entry(_mut) to 'a. This makes the returned value valid for the lifetime of the hash map which makes returning them valid.

Example

use hashbrown::HashMap;

fn get_and_insert<'a>(map: &'a mut HashMap<String, String>, key: String, value: String) -> &'a String {
    map.raw_entry_mut()
        .from_key(&key)
        .insert(key, value)
        get()
}

@Amanieu
Copy link
Member

Amanieu commented Nov 29, 2024

This is incorrect because it would cause the returned lifetime to still be valid even after the entry is removed.

@clarfonthey
Copy link
Contributor

Yeah, this is why you need into_key_mut to get the original lifetimes back.

@ChosenName ChosenName closed this Nov 29, 2024
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

Successfully merging this pull request may close these issues.

3 participants