Skip to content

Commit

Permalink
Add impl
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamGS committed Dec 10, 2024
1 parent a3491d4 commit 8463b34
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "inline-str"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["Adam Gutglick <[email protected]>"]
description = "Efficent and immutable string type, backed by inline-array"
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ impl std::fmt::Debug for InlineStr {
}
}

impl std::hash::Hash for InlineStr {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let as_str: &str = &*self;
as_str.hash(state);
}
}

impl From<String> for InlineStr {
fn from(value: String) -> Self {
Self {
Expand Down Expand Up @@ -107,6 +114,8 @@ impl PartialEq<InlineStr> for Cow<'_, str> {

#[cfg(test)]
mod tests {
use std::hash::{BuildHasher, RandomState};

use super::*;

#[test]
Expand All @@ -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);
}
}

0 comments on commit 8463b34

Please sign in to comment.