diff --git a/integ-tests/tests/de.rs b/integ-tests/tests/de.rs index 8f0cbc0..a2bfca9 100644 --- a/integ-tests/tests/de.rs +++ b/integ-tests/tests/de.rs @@ -60,7 +60,7 @@ impl<'de> Deserialize<'de> for Package { ValueInner::Table(tab) => { let mut th = TableHelper::from((tab, value.span)); - if let Some(mut val) = th.table.remove(&"crate".into()) { + if let Some(mut val) = th.table.remove("crate") { let (name, version) = match val.take() { ValueInner::String(s) => from_str(s), found => { diff --git a/toml-span/src/de_helpers.rs b/toml-span/src/de_helpers.rs index 22ae80d..163b68f 100644 --- a/toml-span/src/de_helpers.rs +++ b/toml-span/src/de_helpers.rs @@ -82,15 +82,15 @@ impl<'de> TableHelper<'de> { /// Returns true if the table contains the specified key #[inline] - pub fn contains(&self, name: &'de str) -> bool { - self.table.contains_key(&name.into()) + pub fn contains(&self, name: &str) -> bool { + self.table.contains_key(name) } /// Takes the specified key and its value if it exists #[inline] pub fn take(&mut self, name: &'static str) -> Option<(value::Key<'de>, Value<'de>)> { self.expected.push(name); - self.table.remove_entry(&name.into()) + self.table.remove_entry(name) } /// Attempts to deserialize the specified key @@ -114,7 +114,7 @@ impl<'de> TableHelper<'de> { ) -> Result, Error> { self.expected.push(name); - let Some(mut val) = self.table.remove(&name.into()) else { + let Some(mut val) = self.table.remove(name) else { let missing = Error { kind: ErrorKind::MissingField(name), span: self.span, @@ -144,7 +144,7 @@ impl<'de> TableHelper<'de> { pub fn optional_s>(&mut self, name: &'static str) -> Option> { self.expected.push(name); - let mut val = self.table.remove(&name.into())?; + let mut val = self.table.remove(name)?; match Spanned::::deserialize(&mut val) { Ok(v) => Some(v), diff --git a/toml-span/src/value.rs b/toml-span/src/value.rs index 14b6aa6..a515a8f 100644 --- a/toml-span/src/value.rs +++ b/toml-span/src/value.rs @@ -66,7 +66,7 @@ impl<'de> Value<'de> { pub fn has_key(&self, key: &str) -> bool { self.value.as_ref().map_or(false, |val| { if let ValueInner::Table(table) = val { - table.contains_key(&key.into()) + table.contains_key(key) } else { false } @@ -144,7 +144,7 @@ impl<'de> Value<'de> { /// Note that this is JSON pointer**-like** because `/` is not supported in /// key names because I don't see the point. If you want this it is easy to /// implement. - pub fn pointer(&self, pointer: &'de str) -> Option<&Self> { + pub fn pointer(&self, pointer: &str) -> Option<&Self> { if pointer.is_empty() { return Some(self); } else if !pointer.starts_with('/') { @@ -157,9 +157,9 @@ impl<'de> Value<'de> { // Don't support / or ~ in key names unless someone actually opens // an issue about it //.map(|x| x.replace("~1", "/").replace("~0", "~")) - .try_fold(self, |target, token| { + .try_fold(self, move |target, token| { (match &target.value { - Some(ValueInner::Table(tab)) => tab.get(&token.into()), + Some(ValueInner::Table(tab)) => tab.get(token), Some(ValueInner::Array(list)) => parse_index(token).and_then(|x| list.get(x)), _ => None, }) @@ -183,7 +183,7 @@ impl<'de> Value<'de> { //.map(|x| x.replace("~1", "/").replace("~0", "~")) .try_fold(self, |target, token| { (match &mut target.value { - Some(ValueInner::Table(tab)) => tab.get_mut(&token.into()), + Some(ValueInner::Table(tab)) => tab.get_mut(token), Some(ValueInner::Array(list)) => { parse_index(token).and_then(|x| list.get_mut(x)) } @@ -225,18 +225,21 @@ pub struct Key<'de> { pub span: Span, } -impl<'de> From<&'de str> for Key<'de> { - fn from(k: &'de str) -> Self { - Self { - name: Cow::Borrowed(k), - span: Span::default(), - } +impl<'de> std::borrow::Borrow for Key<'de> { + fn borrow(&self) -> &str { + self.name.as_ref() } } impl<'de> fmt::Debug for Key<'de> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.name) + f.write_str(&self.name) + } +} + +impl<'de> fmt::Display for Key<'de> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.name) } }