Skip to content

Commit

Permalink
Add ::insert to Entry, RustcEntry, and RawEntryMut
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Oct 4, 2019
1 parent f96b6b9 commit ecbdeb9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,33 @@ where
}

impl<'a, K, V, S> RawEntryMut<'a, K, V, S> {
/// Sets the value of the entry, and returns a RawOccupiedEntryMut.
///
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
///
/// let mut map: HashMap<&str, u32> = HashMap::new();
/// let entry = map.raw_entry_mut().from_key("horseyland").insert("horseyland", 37);
///
/// assert_eq!(entry.remove_entry(), ("horseyland", 37));
/// ```
#[inline]
pub fn insert(self, key: K, value: V) -> RawOccupiedEntryMut<'a, K, V>
where
K: Hash,
S: BuildHasher,
{
match self {
RawEntryMut::Occupied(mut entry) => {
entry.insert(value);
entry
}
RawEntryMut::Vacant(entry) => entry.insert_entry(key, value),
}
}

/// Ensures a value is in the entry by inserting the default if empty, and returns
/// mutable references to the key and value in the entry.
///
Expand Down Expand Up @@ -1674,6 +1701,25 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
(k, v)
}
}

#[inline]
fn insert_entry(self, key: K, value: V) -> RawOccupiedEntryMut<'a, K, V>
where
K: Hash,
S: BuildHasher,
{
let hash_builder = self.hash_builder;
let mut hasher = self.hash_builder.build_hasher();
key.hash(&mut hasher);

let elem = self.table.insert(hasher.finish(), (key, value), |k| {
make_hash(hash_builder, &k.0)
});
RawOccupiedEntryMut {
elem,
table: self.table,
}
}
}

impl<K, V, S> Debug for RawEntryBuilderMut<'_, K, V, S> {
Expand Down Expand Up @@ -2018,6 +2064,33 @@ where
}

impl<'a, K, V, S> Entry<'a, K, V, S> {
/// Sets the value of the entry, and returns an OccupiedEntry.
///
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
///
/// let mut map: HashMap<&str, u32> = HashMap::new();
/// let entry = map.entry("horseyland").insert(37);
///
/// assert_eq!(entry.key(), &"horseyland");
/// ```
#[inline]
pub fn insert(self, value: V) -> OccupiedEntry<'a, K, V, S>
where
K: Hash,
S: BuildHasher,
{
match self {
Entry::Occupied(mut entry) => {
entry.insert(value);
entry
}
Entry::Vacant(entry) => entry.insert_entry(value),
}
}

/// Ensures a value is in the entry by inserting the default if empty, and returns
/// a mutable reference to the value in the entry.
///
Expand Down Expand Up @@ -2449,6 +2522,23 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
});
unsafe { &mut bucket.as_mut().1 }
}

#[inline]
fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V, S>
where
K: Hash,
S: BuildHasher,
{
let hash_builder = &self.table.hash_builder;
let elem = self.table.table.insert(self.hash, (self.key, value), |x| {
make_hash(hash_builder, &x.0)
});
OccupiedEntry {
key: None,
elem,
table: self.table,
}
}
}

impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
Expand Down
22 changes: 22 additions & 0 deletions src/rustc_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,28 @@ impl<K: Debug, V> Debug for RustcVacantEntry<'_, K, V> {
}

impl<'a, K, V> RustcEntry<'a, K, V> {
/// Sets the value of the entry, and returns a RustcOccupiedEntry.
///
/// # Examples
///
/// ```
/// use hashbrown::HashMap;
///
/// let mut map: HashMap<&str, u32> = HashMap::new();
/// let entry = map.entry("horseyland").insert(37);
///
/// assert_eq!(entry.key(), &"horseyland");
/// ```
pub fn insert(self, value: V) -> RustcOccupiedEntry<'a, K, V> {
match self {
Vacant(entry) => entry.insert_entry(value),
Occupied(mut entry) => {
entry.insert(value);
entry
}
}
}

/// Ensures a value is in the entry by inserting the default if empty, and returns
/// a mutable reference to the value in the entry.
///
Expand Down

0 comments on commit ecbdeb9

Please sign in to comment.