@@ -149,6 +149,22 @@ impl<Static: StaticAtomSet> Atom<Static> {
149149 _ => unsafe { debug_unreachable ! ( ) } ,
150150 }
151151 }
152+
153+ pub fn try_static ( string_to_add : & str ) -> Option < Self > {
154+ Self :: try_static_internal ( string_to_add) . ok ( )
155+ }
156+
157+ fn try_static_internal ( string_to_add : & str ) -> Result < Self , phf_shared:: Hashes > {
158+ let static_set = Static :: get ( ) ;
159+ let hash = phf_shared:: hash ( & * string_to_add, & static_set. key ) ;
160+ let index = phf_shared:: get_index ( & hash, static_set. disps , static_set. atoms . len ( ) ) ;
161+
162+ if static_set. atoms [ index as usize ] == string_to_add {
163+ Ok ( Self :: pack_static ( index) )
164+ } else {
165+ Err ( hash)
166+ }
167+ }
152168}
153169
154170impl < Static : StaticAtomSet > Default for Atom < Static > {
@@ -170,13 +186,7 @@ impl<Static: StaticAtomSet> Hash for Atom<Static> {
170186
171187impl < ' a , Static : StaticAtomSet > From < Cow < ' a , str > > for Atom < Static > {
172188 fn from ( string_to_add : Cow < ' a , str > ) -> Self {
173- let static_set = Static :: get ( ) ;
174- let hash = phf_shared:: hash ( & * string_to_add, & static_set. key ) ;
175- let index = phf_shared:: get_index ( & hash, static_set. disps , static_set. atoms . len ( ) ) ;
176-
177- if static_set. atoms [ index as usize ] == string_to_add {
178- Self :: pack_static ( index)
179- } else {
189+ Self :: try_static_internal ( & * string_to_add) . unwrap_or_else ( |hash| {
180190 let len = string_to_add. len ( ) ;
181191 if len <= MAX_INLINE_LEN {
182192 let mut data: u64 = ( INLINE_TAG as u64 ) | ( ( len as u64 ) << LEN_OFFSET ) ;
@@ -200,7 +210,7 @@ impl<'a, Static: StaticAtomSet> From<Cow<'a, str>> for Atom<Static> {
200210 phantom : PhantomData ,
201211 }
202212 }
203- }
213+ } )
204214 }
205215}
206216
0 commit comments