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<Spanned<String>, T> with a dummy span and get results where only the content has to match. * Add Borrow impl * Add tests
This commit is contained in:
parent
112fb15498
commit
e89d255cbf
|
@ -1,5 +1,8 @@
|
||||||
use serde::{de, ser};
|
use serde::{de, ser};
|
||||||
|
use std::borrow::Borrow;
|
||||||
|
use std::cmp::Ordering;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
pub(crate) const NAME: &str = "$__toml_private_Spanned";
|
pub(crate) const NAME: &str = "$__toml_private_Spanned";
|
||||||
pub(crate) const START: &str = "$__toml_private_start";
|
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"));
|
/// assert_eq!(u.s.into_inner(), String::from("value"));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Spanned<T> {
|
pub struct Spanned<T> {
|
||||||
/// The start range.
|
/// The start range.
|
||||||
start: usize,
|
start: usize,
|
||||||
|
@ -70,6 +73,38 @@ impl<T> Spanned<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Borrow<str> for Spanned<String> {
|
||||||
|
fn borrow(&self) -> &str {
|
||||||
|
&self.get_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: PartialEq> PartialEq for Spanned<T> {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.value.eq(&other.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Eq> Eq for Spanned<T> {}
|
||||||
|
|
||||||
|
impl<T: Hash> Hash for Spanned<T> {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
self.value.hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: PartialOrd> PartialOrd for Spanned<T> {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
self.value.partial_cmp(&other.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Ord> Ord for Spanned<T> {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
self.value.cmp(&other.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, T> de::Deserialize<'de> for Spanned<T>
|
impl<'de, T> de::Deserialize<'de> for Spanned<T>
|
||||||
where
|
where
|
||||||
T: de::Deserialize<'de>,
|
T: de::Deserialize<'de>,
|
||||||
|
|
41
test-suite/tests/spanned-impls.rs
Normal file
41
test-suite/tests/spanned-impls.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use std::cmp::{Ord, Ordering, PartialOrd};
|
||||||
|
use toml::{from_str, Spanned};
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_spans_impls() {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Foo {
|
||||||
|
bar: Spanned<bool>,
|
||||||
|
baz: Spanned<String>,
|
||||||
|
}
|
||||||
|
let f: Foo = from_str(
|
||||||
|
"
|
||||||
|
bar = true
|
||||||
|
baz = \"yes\"
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let g: Foo = from_str(
|
||||||
|
"
|
||||||
|
baz = \"yes\"
|
||||||
|
bar = true
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(f.bar.span() != g.bar.span());
|
||||||
|
assert!(f.baz.span() != g.baz.span());
|
||||||
|
|
||||||
|
// test that eq still holds
|
||||||
|
assert_eq!(f.bar, g.bar);
|
||||||
|
assert_eq!(f.baz, g.baz);
|
||||||
|
|
||||||
|
// test that Ord returns equal order
|
||||||
|
assert_eq!(f.bar.cmp(&g.bar), Ordering::Equal);
|
||||||
|
assert_eq!(f.baz.cmp(&g.baz), Ordering::Equal);
|
||||||
|
|
||||||
|
// test that PartialOrd returns equal order
|
||||||
|
assert_eq!(f.bar.partial_cmp(&g.bar), Some(Ordering::Equal));
|
||||||
|
assert_eq!(f.baz.partial_cmp(&g.baz), Some(Ordering::Equal));
|
||||||
|
}
|
Loading…
Reference in a new issue