Skip to content

Commit b700522

Browse files
authored
Merge pull request #411 from ya7010/add_get_key_value_mut
feat: add IndexMap::get_key_value_mut
2 parents 48a98b7 + 01f3ef0 commit b700522

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/map.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ where
828828
self.get_index_of(key).is_some()
829829
}
830830

831-
/// Return a reference to the value stored for `key`, if it is present,
831+
/// Return a reference to the stored value for `key`, if it is present,
832832
/// else `None`.
833833
///
834834
/// Computes in **O(1)** time (average).
@@ -844,7 +844,7 @@ where
844844
}
845845
}
846846

847-
/// Return references to the key-value pair stored for `key`,
847+
/// Return references to the stored key-value pair for the lookup `key`,
848848
/// if it is present, else `None`.
849849
///
850850
/// Computes in **O(1)** time (average).
@@ -860,7 +860,10 @@ where
860860
}
861861
}
862862

863-
/// Return item index, key and value
863+
/// Return the index with references to the stored key-value pair for the
864+
/// lookup `key`, if it is present, else `None`.
865+
///
866+
/// Computes in **O(1)** time (average).
864867
pub fn get_full<Q>(&self, key: &Q) -> Option<(usize, &K, &V)>
865868
where
866869
Q: ?Sized + Hash + Equivalent<K>,
@@ -873,7 +876,7 @@ where
873876
}
874877
}
875878

876-
/// Return item index, if it exists in the map
879+
/// Return the item index for `key`, if it is present, else `None`.
877880
///
878881
/// Computes in **O(1)** time (average).
879882
pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
@@ -890,6 +893,10 @@ where
890893
}
891894
}
892895

896+
/// Return a mutable reference to the stored value for `key`,
897+
/// if it is present, else `None`.
898+
///
899+
/// Computes in **O(1)** time (average).
893900
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
894901
where
895902
Q: ?Sized + Hash + Equivalent<K>,
@@ -902,6 +909,26 @@ where
902909
}
903910
}
904911

912+
/// Return a reference and mutable references to the stored key-value pair
913+
/// for the lookup `key`, if it is present, else `None`.
914+
///
915+
/// Computes in **O(1)** time (average).
916+
pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
917+
where
918+
Q: ?Sized + Hash + Equivalent<K>,
919+
{
920+
if let Some(i) = self.get_index_of(key) {
921+
let entry = &mut self.as_entries_mut()[i];
922+
Some((&entry.key, &mut entry.value))
923+
} else {
924+
None
925+
}
926+
}
927+
928+
/// Return the index with a reference and mutable reference to the stored
929+
/// key-value pair for the lookup `key`, if it is present, else `None`.
930+
///
931+
/// Computes in **O(1)** time (average).
905932
pub fn get_full_mut<Q>(&mut self, key: &Q) -> Option<(usize, &K, &mut V)>
906933
where
907934
Q: ?Sized + Hash + Equivalent<K>,

0 commit comments

Comments
 (0)