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

Change key to return &K rather than &Q #425

Merged
merged 2 commits into from
Apr 21, 2023
Merged
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
19 changes: 7 additions & 12 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4562,7 +4562,7 @@ impl<K: Borrow<Q>, Q: ?Sized + Debug, V: Debug, S, A: Allocator + Clone> Debug
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OccupiedEntryRef")
.field("key", &self.key())
.field("key", &self.key().borrow())
.field("value", &self.get())
.finish()
}
Expand Down Expand Up @@ -5832,7 +5832,7 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator + Clone> EntryRef<'a, 'b, K, Q, V,
K: Borrow<Q>,
{
match *self {
EntryRef::Occupied(ref entry) => entry.key(),
EntryRef::Occupied(ref entry) => entry.key().borrow(),
EntryRef::Vacant(ref entry) => entry.key(),
}
}
Expand Down Expand Up @@ -5928,8 +5928,7 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator + Clone> EntryRef<'a, 'b, K, Q, V,
#[cfg_attr(feature = "inline-more", inline)]
pub fn and_replace_entry_with<F>(self, f: F) -> Self
where
F: FnOnce(&Q, V) -> Option<V>,
K: Borrow<Q>,
F: FnOnce(&K, V) -> Option<V>,
{
match self {
EntryRef::Occupied(entry) => entry.replace_entry_with(f),
Expand Down Expand Up @@ -5988,11 +5987,8 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator + Clone> OccupiedEntryRef<'a, 'b,
/// }
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn key(&self) -> &Q
where
K: Borrow<Q>,
{
unsafe { &self.elem.as_ref().0 }.borrow()
pub fn key(&self) -> &K {
unsafe { &self.elem.as_ref().0 }
}

/// Take the ownership of the key and value from the map.
Expand Down Expand Up @@ -6297,16 +6293,15 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator + Clone> OccupiedEntryRef<'a, 'b,
#[cfg_attr(feature = "inline-more", inline)]
pub fn replace_entry_with<F>(self, f: F) -> EntryRef<'a, 'b, K, Q, V, S, A>
where
F: FnOnce(&Q, V) -> Option<V>,
K: Borrow<Q>,
F: FnOnce(&K, V) -> Option<V>,
{
unsafe {
let mut spare_key = None;

self.table
.table
.replace_bucket_with(self.elem.clone(), |(key, value)| {
if let Some(new_value) = f(key.borrow(), value) {
if let Some(new_value) = f(&key, value) {
Some((key, new_value))
} else {
spare_key = Some(KeyOrRef::Owned(key));
Expand Down