Skip to content

Commit

Permalink
Document all entry methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Jan 20, 2021
1 parent 29a4f19 commit 0d801a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/map/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ pub enum Entry<'a, K, V> {
}

impl<'a, K, V> Entry<'a, K, V> {
/// Inserts the given default value in the entry if it is vacant and returns a mutable
/// reference to it. Otherwise a mutable reference to an already existent value is returned.
///
/// Computes in **O(1)** time (amortized average).
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Expand All @@ -453,6 +456,9 @@ impl<'a, K, V> Entry<'a, K, V> {
}
}

/// Inserts the result of the `call` function in the entry if it is vacant and returns a mutable
/// reference to it. Otherwise a mutable reference to an already existent value is returned.
///
/// Computes in **O(1)** time (amortized average).
pub fn or_insert_with<F>(self, call: F) -> &'a mut V
where
Expand Down Expand Up @@ -590,6 +596,7 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
&self.key
}

/// Takes ownership of the key, leaving the entry vacant.
pub fn into_key(self) -> K {
self.key
}
Expand All @@ -599,6 +606,8 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
self.map.len()
}

/// Inserts the entry's key and the given value into the map, and returns a mutable reference
/// to the value.
pub fn insert(self, value: V) -> &'a mut V {
let i = self.map.push(self.hash, self.key, value);
&mut self.map.entries[i].value
Expand Down
7 changes: 7 additions & 0 deletions src/map/core/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,15 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
&self.map.entries[self.index()].key
}

/// Gets a reference to the entry's value in the map.
pub fn get(&self) -> &V {
&self.map.entries[self.index()].value
}

/// Gets a mutable reference to the entry's value in the map.
///
/// If you need a reference which may outlive the destruction of the
/// `Entry` value, see `into_mut`.
pub fn get_mut(&mut self) -> &mut V {
let index = self.index();
&mut self.map.entries[index].value
Expand All @@ -136,6 +141,8 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
unsafe { self.raw_bucket.read() }
}

/// Converts into a mutable reference to the entry's value in the map,
/// with a lifetime bound to the map itself.
pub fn into_mut(self) -> &'a mut V {
let index = self.index();
&mut self.map.entries[index].value
Expand Down

0 comments on commit 0d801a6

Please sign in to comment.