Skip to content

Commit

Permalink
added more efficient and more generic implementation of PartialEq for…
Browse files Browse the repository at this point in the history
… StringInterner
  • Loading branch information
Robbepop committed Jul 9, 2017
1 parent 2240737 commit 18adc7a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub type DefaultStringInterner = StringInterner<usize>;
///
/// The main goal of this `StringInterner` is to store String
/// with as low memory overhead as possible.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Eq)]
pub struct StringInterner<Sym, H = RandomState>
where Sym: Symbol,
H : BuildHasher
Expand All @@ -172,6 +172,15 @@ pub struct StringInterner<Sym, H = RandomState>
values: Vec<Box<str>>
}

impl<Sym, H> PartialEq for StringInterner<Sym, H>
where Sym: Symbol,
H : BuildHasher
{
fn eq(&self, rhs: &Self) -> bool {
self.len() == rhs.len() && self.values == rhs.values
}
}

impl Default for StringInterner<usize, RandomState> {
#[inline]
fn default() -> Self {
Expand Down
9 changes: 1 addition & 8 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,5 @@ fn serde() {
let (interner, _) = make_dummy_interner();
let serialized = serde_json::to_string(&interner).unwrap();
let deserialized: DefaultStringInterner = serde_json::from_str(&serialized).unwrap();
// assert_eq!(interner, deserialized);
let mut it = deserialized.into_iter();
assert_eq!(it.next(), Some((0, "foo".to_owned())));
assert_eq!(it.next(), Some((1, "bar".to_owned())));
assert_eq!(it.next(), Some((2, "baz".to_owned())));
assert_eq!(it.next(), Some((3, "rofl".to_owned())));
assert_eq!(it.next(), Some((4, "mao".to_owned())));
assert_eq!(it.next(), None);
assert_eq!(interner, deserialized);
}

0 comments on commit 18adc7a

Please sign in to comment.