diff --git a/Cargo.toml b/Cargo.toml index 1f0897b..8599ad7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "inline-str" -version = "0.3.0" +version = "0.4.0" edition = "2021" authors = ["Adam Gutglick "] description = "Efficent and immutable string type, backed by inline-array" diff --git a/src/lib.rs b/src/lib.rs index 9dedeb0..b119060 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,13 @@ impl std::fmt::Debug for InlineStr { } } +impl std::hash::Hash for InlineStr { + fn hash(&self, state: &mut H) { + let as_str: &str = &*self; + as_str.hash(state); + } +} + impl From for InlineStr { fn from(value: String) -> Self { Self { @@ -107,6 +114,8 @@ impl PartialEq for Cow<'_, str> { #[cfg(test)] mod tests { + use std::hash::{BuildHasher, RandomState}; + use super::*; #[test] @@ -118,4 +127,19 @@ mod tests { assert_eq!(words, inline_words); assert_eq!(inline_words, words); } + + #[test] + fn test_basic_hash() { + let hasher = RandomState::new(); + + let words = "the quick brown fox"; + let inline_words = InlineStr::from(words); + + let words_hash = hasher.hash_one(words); + let words_hash_2 = hasher.hash_one(words); + let inline_hash = hasher.hash_one(inline_words); + + assert_eq!(words_hash, words_hash_2); + assert_eq!(words_hash, inline_hash); + } }