From ed6f34ab8c98e9ed6b4a3858001cd81893e81582 Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 6 Oct 2023 16:57:18 +0100 Subject: [PATCH] Switch from jget etc to AsBorrowed trait --- crates/valence_nbt/src/compound.rs | 122 ++++++++++++++++------------- 1 file changed, 66 insertions(+), 56 deletions(-) diff --git a/crates/valence_nbt/src/compound.rs b/crates/valence_nbt/src/compound.rs index 0be11f438..3b47e7f5d 100644 --- a/crates/valence_nbt/src/compound.rs +++ b/crates/valence_nbt/src/compound.rs @@ -1,4 +1,4 @@ -use std::borrow::Borrow; +use std::borrow::{Borrow, Cow}; use std::fmt; use std::hash::Hash; use std::iter::FusedIterator; @@ -95,34 +95,38 @@ where { pub fn get(&self, k: &Q) -> Option<&Value> where - S: Borrow, - Q: ?Sized + Eq + Ord + Hash, + Q: ?Sized + AsBorrowed, + >::Borrowed: Hash + Eq + Ord, + S: Borrow<>::Borrowed>, { - self.map.get(k) + self.map.get(k.as_borrowed()) } pub fn contains_key(&self, k: &Q) -> bool where - S: Borrow, - Q: ?Sized + Eq + Ord + Hash, + Q: ?Sized + AsBorrowed, + >::Borrowed: Hash + Eq + Ord, + S: Borrow<>::Borrowed>, { - self.map.contains_key(k) + self.map.contains_key(k.as_borrowed()) } pub fn get_mut(&mut self, k: &Q) -> Option<&mut Value> where - S: Borrow, - Q: ?Sized + Eq + Ord + Hash, + Q: ?Sized + AsBorrowed, + >::Borrowed: Hash + Eq + Ord, + S: Borrow<>::Borrowed>, { - self.map.get_mut(k) + self.map.get_mut(k.as_borrowed()) } pub fn get_key_value(&self, k: &Q) -> Option<(&S, &Value)> where - S: Borrow, - Q: ?Sized + Eq + Ord + Hash, + Q: ?Sized + AsBorrowed, + >::Borrowed: Hash + Eq + Ord, + S: Borrow<>::Borrowed>, { - self.map.get_key_value(k) + self.map.get_key_value(k.as_borrowed()) } pub fn insert(&mut self, k: K, v: V) -> Option> @@ -135,10 +139,11 @@ where pub fn remove(&mut self, k: &Q) -> Option> where - S: Borrow, - Q: ?Sized + Eq + Ord + Hash, + Q: ?Sized + AsBorrowed, + >::Borrowed: Hash + Eq + Ord, + S: Borrow<>::Borrowed>, { - self.map.remove(k) + self.map.remove(k.as_borrowed()) } pub fn remove_entry(&mut self, k: &Q) -> Option<(S, Value)> @@ -274,54 +279,59 @@ where } } -#[cfg(feature = "java_string")] -impl Compound { - pub fn jget<'a, Q>(&self, k: Q) -> Option<&Value> - where - Q: Into<&'a java_string::JavaStr>, - { - self.get(k.into()) - } +/// Trait that can be used as a key to query a compound. Basically something that can be converted +/// to a type `B` such that `S: Borrow`. +pub trait AsBorrowed { + type Borrowed: ?Sized; - pub fn jcontains_key<'a, Q>(&self, k: Q) -> bool - where - Q: Into<&'a java_string::JavaStr>, - { - self.contains_key(k.into()) - } + fn as_borrowed(&self) -> &Self::Borrowed; +} - pub fn jget_mut<'a, Q>(&mut self, k: Q) -> Option<&mut Value> - where - Q: Into<&'a java_string::JavaStr>, - { - self.get_mut(k.into()) +impl AsBorrowed for Q +where + String: Borrow, +{ + type Borrowed = Q; + + #[inline] + fn as_borrowed(&self) -> &Q { + self } +} - pub fn jget_key_value<'a, Q>( - &self, - k: Q, - ) -> Option<(&java_string::JavaString, &Value)> - where - Q: Into<&'a java_string::JavaStr>, - { - self.get_key_value(k.into()) +impl<'a, Q: ?Sized> AsBorrowed> for Q +where + Cow<'a, str>: Borrow, +{ + type Borrowed = Q; + + #[inline] + fn as_borrowed(&self) -> &Q { + self } +} - pub fn jremove<'a, Q>(&mut self, k: Q) -> Option> - where - Q: Into<&'a java_string::JavaStr>, - { - self.remove(k.into()) +#[cfg(feature = "java_string")] +impl AsBorrowed for Q +where + for<'a> &'a Q: Into<&'a java_string::JavaStr>, +{ + type Borrowed = java_string::JavaStr; + + fn as_borrowed(&self) -> &Self::Borrowed { + self.into() } +} - pub fn jremove_entry<'a, Q>( - &mut self, - k: Q, - ) -> Option<(java_string::JavaString, Value)> - where - Q: Into<&'a java_string::JavaStr>, - { - self.remove_entry(k.into()) +#[cfg(feature = "java_string")] +impl AsBorrowed> for Q +where + for<'a> &'a Q: Into<&'a java_string::JavaStr>, +{ + type Borrowed = java_string::JavaStr; + + fn as_borrowed(&self) -> &Self::Borrowed { + self.into() } }