Skip to content

Commit

Permalink
fix indexmap deprication warnings (#598)
Browse files Browse the repository at this point in the history
# Objective

IndexMap remove methods are deprecated in favor of [swap|shift]_remove

# Solution

match serde_json's implementation of swap and shift remove

---------

Co-authored-by: AviiNL <[email protected]>
  • Loading branch information
AviiNL and AviiNL authored Jan 29, 2024
1 parent 5123826 commit 5dead53
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ glam = "0.25.0"
heck = "0.5.0-rc.1"
hmac = "0.12.1"
image = "0.24.6"
indexmap = "2.0.0"
indexmap = "2.2.1"
itertools = "0.12.0"
java_string = { path = "crates/java_string", version = "0.1.2" }
lru = "0.12.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/valence_entity/src/active_status_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl ActiveStatusEffects {

/// Removes an effect.
fn remove_effect(&mut self, effect: StatusEffect) {
self.current_effects.remove(&effect);
self.current_effects.swap_remove(&effect);
}

/// Removes all effects.
Expand Down
6 changes: 3 additions & 3 deletions crates/valence_entity/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ impl EntityAttributeInstance {

/// Removes a modifier.
pub fn remove_modifier(&mut self, uuid: Uuid) {
self.add_modifiers.remove(&uuid);
self.multiply_base_modifiers.remove(&uuid);
self.multiply_total_modifiers.remove(&uuid);
self.add_modifiers.swap_remove(&uuid);
self.multiply_base_modifiers.swap_remove(&uuid);
self.multiply_total_modifiers.swap_remove(&uuid);
}

/// Clears all modifiers.
Expand Down
63 changes: 60 additions & 3 deletions crates/valence_nbt/src/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,59 @@ where
<Q as AsBorrowed<S>>::Borrowed: Hash + Ord,
S: Borrow<<Q as AsBorrowed<S>>::Borrowed>,
{
self.map.remove(k.as_borrowed())
#[cfg(feature = "preserve_order")]
return self.swap_remove(k);
#[cfg(not(feature = "preserve_order"))]
return self.map.remove(k.as_borrowed());
}

#[cfg(feature = "preserve_order")]
pub fn swap_remove<Q>(&mut self, k: &Q) -> Option<Value<S>>
where
Q: ?Sized + AsBorrowed<S>,
<Q as AsBorrowed<S>>::Borrowed: Hash + Ord,
S: Borrow<<Q as AsBorrowed<S>>::Borrowed>,
{
self.map.swap_remove(k.as_borrowed())
}

#[cfg(feature = "preserve_order")]
pub fn shift_remove<Q>(&mut self, k: &Q) -> Option<Value<S>>
where
Q: ?Sized + AsBorrowed<S>,
<Q as AsBorrowed<S>>::Borrowed: Hash + Ord,
S: Borrow<<Q as AsBorrowed<S>>::Borrowed>,
{
self.map.shift_remove(k.as_borrowed())
}

pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(S, Value<S>)>
where
S: Borrow<Q>,
Q: ?Sized + Ord + Hash,
{
self.map.remove_entry(k)
#[cfg(feature = "preserve_order")]
return self.swap_remove_entry(k);
#[cfg(not(feature = "preserve_order"))]
return self.map.remove_entry(k);
}

#[cfg(feature = "preserve_order")]
pub fn swap_remove_entry<Q>(&mut self, k: &Q) -> Option<(S, Value<S>)>
where
S: Borrow<Q>,
Q: ?Sized + Ord + Hash,
{
self.map.swap_remove_entry(k)
}

#[cfg(feature = "preserve_order")]
pub fn shift_remove_entry<Q>(&mut self, k: &Q) -> Option<(S, Value<S>)>
where
S: Borrow<Q>,
Q: ?Sized + Ord + Hash,
{
self.map.shift_remove_entry(k)
}

pub fn append(&mut self, other: &mut Self) {
Expand Down Expand Up @@ -461,7 +505,20 @@ where
}

pub fn remove(self) -> Value<S> {
self.oe.remove()
#[cfg(feature = "preserve_order")]
return self.swap_remove();
#[cfg(not(feature = "preserve_order"))]
return self.oe.remove();
}

#[cfg(feature = "preserve_order")]
pub fn swap_remove(self) -> Value<S> {
self.oe.swap_remove()
}

#[cfg(feature = "preserve_order")]
pub fn shift_remove(self) -> Value<S> {
self.oe.shift_remove()
}
}

Expand Down

0 comments on commit 5dead53

Please sign in to comment.