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

Add an assertion to HashSet::get_or_insert_with #518

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 16 additions & 1 deletion src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ where
/// Inserts a value computed from `f` into the set if the given `value` is
/// not present, then returns a reference to the value in the set.
///
/// # Panics
///
/// Will panic if `f`'s return value is not equivalent to the query `value`.
///
/// # Examples
///
/// ```
Expand All @@ -970,6 +974,13 @@ where
/// }
/// assert_eq!(set.len(), 4); // a new "fish" was inserted
/// ```
///
/// The following example will panic because the new value doesn't match.
///
/// ```should_panic
/// let mut set = hashbrown::HashSet::new();
/// set.get_or_insert_with("rust", |_| String::new());
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
where
Expand All @@ -981,7 +992,11 @@ where
self.map
.raw_entry_mut()
.from_key(value)
.or_insert_with(|| (f(value), ()))
.or_insert_with(|| {
let new = f(value);
assert!(value.equivalent(&new), "new value is not equivalent");
(new, ())
})
.0
}

Expand Down
Loading