From 8995cef9d6c347c275ea6bbbe0c45523df1b1314 Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 25 Oct 2019 21:05:31 +0200 Subject: Spanned: impl PartialEq, Eq, Hash, PartialOrd, Ord in terms of the value (#344) * Spanned: impl PartialEq, Eq, Hash, PartialOrd, Ord in terms of the value This is because we want to be able to index into HashMap, T> with a dummy span and get results where only the content has to match. * Add Borrow impl * Add tests --- src/spanned.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/spanned.rs b/src/spanned.rs index e3c2774..9ee56ae 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -1,5 +1,8 @@ use serde::{de, ser}; +use std::borrow::Borrow; +use std::cmp::Ordering; use std::fmt; +use std::hash::{Hash, Hasher}; pub(crate) const NAME: &str = "$__toml_private_Spanned"; pub(crate) const START: &str = "$__toml_private_start"; @@ -28,7 +31,7 @@ pub(crate) const VALUE: &str = "$__toml_private_value"; /// assert_eq!(u.s.into_inner(), String::from("value")); /// } /// ``` -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Debug)] pub struct Spanned { /// The start range. start: usize, @@ -70,6 +73,38 @@ impl Spanned { } } +impl Borrow for Spanned { + fn borrow(&self) -> &str { + &self.get_ref() + } +} + +impl PartialEq for Spanned { + fn eq(&self, other: &Self) -> bool { + self.value.eq(&other.value) + } +} + +impl Eq for Spanned {} + +impl Hash for Spanned { + fn hash(&self, state: &mut H) { + self.value.hash(state); + } +} + +impl PartialOrd for Spanned { + fn partial_cmp(&self, other: &Self) -> Option { + self.value.partial_cmp(&other.value) + } +} + +impl Ord for Spanned { + fn cmp(&self, other: &Self) -> Ordering { + self.value.cmp(&other.value) + } +} + impl<'de, T> de::Deserialize<'de> for Spanned where T: de::Deserialize<'de>, -- cgit v1.2.3