Skip to content

Added or_insert_with_key methods to Entry types #16

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/weak_key_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,24 @@ impl<'a, K: WeakKey, V> Entry<'a, K, V> {
}
}

/// Ensures a value is in the entry by inserting, if empty, the result of the default function.
/// This method allows for generating key-derived values for insertion by providing the default
/// function a reference to the key that was moved during the `.entry(key)` method call.
///
/// The reference to the moved key is provided so that cloning or copying the key is
/// unnecessary, unlike with `.or_insert_with(|| ... )`.
///
/// *O*(1) time
pub fn or_insert_with_key<F: FnOnce(&K::Strong) -> V>(self, default: F) -> &'a mut V {
match self {
Entry::Occupied(occupied) => occupied.into_mut(),
Entry::Vacant(vacant) => {
let value = default(vacant.key());
vacant.insert(value)
},
}
}

/// Returns a reference to this entry's key.
///
/// *O*(1) time
Expand Down
18 changes: 18 additions & 0 deletions src/weak_value_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,24 @@ impl<'a, K, V: WeakElement> Entry<'a, K, V> {
}
}

/// Ensures a value is in the entry by inserting, if empty, the result of the default function.
/// This method allows for generating key-derived values for insertion by providing the default
/// function a reference to the key that was moved during the `.entry(key)` method call.
///
/// The reference to the moved key is provided so that cloning or copying the key is
/// unnecessary, unlike with `.or_insert_with(|| ... )`.
///
/// *O*(1) time
pub fn or_insert_with_key<F: FnOnce(&K) -> V::Strong>(self, default: F) -> V::Strong {
match self {
Entry::Occupied(occupied) => occupied.get_strong(),
Entry::Vacant(vacant) => {
let value = default(vacant.key());
vacant.insert(value)
},
}
}

/// Returns a reference to this entry's key.
///
/// *O*(1) time
Expand Down
18 changes: 18 additions & 0 deletions src/weak_weak_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,24 @@ impl<'a, K: WeakKey, V: WeakElement> Entry<'a, K, V> {
}
}

/// Ensures a value is in the entry by inserting, if empty, the result of the default function.
/// This method allows for generating key-derived values for insertion by providing the default
/// function a reference to the key that was moved during the `.entry(key)` method call.
///
/// The reference to the moved key is provided so that cloning or copying the key is
/// unnecessary, unlike with `.or_insert_with(|| ... )`.
///
/// *O*(1) time
pub fn or_insert_with_key<F: FnOnce(&K::Strong) -> V::Strong>(self, default: F) -> V::Strong {
match self {
Entry::Occupied(occupied) => occupied.get_strong(),
Entry::Vacant(vacant) => {
let value = default(vacant.key());
vacant.insert(value)
},
}
}

/// Returns a reference to this entry's key.
///
/// *O*(1) time
Expand Down