Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ fn test_from_string() {
assert!(Atom::from("camembert".to_owned()) == Atom::from("camembert"));
}

#[test]
fn test_try_static() {
assert!(Atom::try_static("head").is_some());
assert!(Atom::try_static("not in the static table").is_none());
}

#[cfg(all(test, feature = "unstable"))]
#[path = "bench.rs"]
mod bench;
26 changes: 18 additions & 8 deletions src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ impl<Static: StaticAtomSet> Atom<Static> {
_ => unsafe { debug_unreachable!() },
}
}

pub fn try_static(string_to_add: &str) -> Option<Self> {
Self::try_static_internal(string_to_add).ok()
}

fn try_static_internal(string_to_add: &str) -> Result<Self, phf_shared::Hashes> {
let static_set = Static::get();
let hash = phf_shared::hash(&*string_to_add, &static_set.key);
let index = phf_shared::get_index(&hash, static_set.disps, static_set.atoms.len());

if static_set.atoms[index as usize] == string_to_add {
Ok(Self::pack_static(index))
} else {
Err(hash)
}
}
}

impl<Static: StaticAtomSet> Default for Atom<Static> {
Expand All @@ -170,13 +186,7 @@ impl<Static: StaticAtomSet> Hash for Atom<Static> {

impl<'a, Static: StaticAtomSet> From<Cow<'a, str>> for Atom<Static> {
fn from(string_to_add: Cow<'a, str>) -> Self {
let static_set = Static::get();
let hash = phf_shared::hash(&*string_to_add, &static_set.key);
let index = phf_shared::get_index(&hash, static_set.disps, static_set.atoms.len());

if static_set.atoms[index as usize] == string_to_add {
Self::pack_static(index)
} else {
Self::try_static_internal(&*string_to_add).unwrap_or_else(|hash| {
let len = string_to_add.len();
if len <= MAX_INLINE_LEN {
let mut data: u64 = (INLINE_TAG as u64) | ((len as u64) << LEN_OFFSET);
Expand All @@ -200,7 +210,7 @@ impl<'a, Static: StaticAtomSet> From<Cow<'a, str>> for Atom<Static> {
phantom: PhantomData,
}
}
}
})
}
}

Expand Down