1010#![ allow( non_upper_case_globals) ]
1111
1212use crate :: dynamic_set:: { Entry , DYNAMIC_SET } ;
13+ use crate :: static_sets:: StaticAtomSet ;
1314use debug_unreachable:: debug_unreachable;
1415use phf_shared;
1516use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
@@ -35,61 +36,6 @@ const LEN_MASK: u64 = 0xF0;
3536const MAX_INLINE_LEN : usize = 7 ;
3637const STATIC_SHIFT_BITS : usize = 32 ;
3738
38- /// A static `PhfStrSet`
39- ///
40- /// This trait is implemented by static sets of interned strings generated using
41- /// `string_cache_codegen`, and `EmptyStaticAtomSet` for when strings will be added dynamically.
42- ///
43- /// It is used by the methods of [`Atom`] to check if a string is present in the static set.
44- ///
45- /// [`Atom`]: struct.Atom.html
46- pub trait StaticAtomSet : Ord {
47- /// Get the location of the static string set in the binary.
48- fn get ( ) -> & ' static PhfStrSet ;
49- /// Get the index of the empty string, which is in every set and is used for `Atom::default`.
50- fn empty_string_index ( ) -> u32 ;
51- }
52-
53- /// A string set created using a [perfect hash function], specifically
54- /// [Hash, Displace and Compress].
55- ///
56- /// See the CHD document for the meaning of the struct fields.
57- ///
58- /// [perfect hash function]: https://en.wikipedia.org/wiki/Perfect_hash_function
59- /// [Hash, Displace and Compress]: http://cmph.sourceforge.net/papers/esa09.pdf
60- pub struct PhfStrSet {
61- pub key : u64 ,
62- pub disps : & ' static [ ( u32 , u32 ) ] ,
63- pub atoms : & ' static [ & ' static str ] ,
64- pub hashes : & ' static [ u32 ] ,
65- }
66-
67- /// An empty static atom set for when only dynamic strings will be added
68- #[ derive( PartialEq , Eq , PartialOrd , Ord ) ]
69- pub struct EmptyStaticAtomSet ;
70-
71- impl StaticAtomSet for EmptyStaticAtomSet {
72- fn get ( ) -> & ' static PhfStrSet {
73- // The name is a lie: this set is not empty (it contains the empty string)
74- // but that’s only to avoid divisions by zero in rust-phf.
75- static SET : PhfStrSet = PhfStrSet {
76- key : 0 ,
77- disps : & [ ( 0 , 0 ) ] ,
78- atoms : & [ "" ] ,
79- // "" SipHash'd, and xored with u64_hash_to_u32.
80- hashes : & [ 0x3ddddef3 ] ,
81- } ;
82- & SET
83- }
84-
85- fn empty_string_index ( ) -> u32 {
86- 0
87- }
88- }
89-
90- /// Use this if you don’t care about static atoms.
91- pub type DefaultAtom = Atom < EmptyStaticAtomSet > ;
92-
9339/// Represents a string that has been interned.
9440///
9541/// While the type definition for `Atom` indicates that it generic on a particular
@@ -277,10 +223,8 @@ impl<'a, Static: StaticAtomSet> From<Cow<'a, str>> for Atom<Static> {
277223 phantom : PhantomData ,
278224 }
279225 } else {
280- let ptr: std:: ptr:: NonNull < Entry > = DYNAMIC_SET
281- . lock ( )
282- . unwrap ( )
283- . insert ( string_to_add, hash. g ) ;
226+ let ptr: std:: ptr:: NonNull < Entry > =
227+ DYNAMIC_SET . lock ( ) . unwrap ( ) . insert ( string_to_add, hash. g ) ;
284228 let data = ptr. as_ptr ( ) as u64 ;
285229 debug_assert ! ( 0 == data & TAG_MASK ) ;
286230 Atom {
@@ -516,29 +460,3 @@ fn inline_atom_slice_mut(x: &mut u64) -> &mut [u8] {
516460 slice:: from_raw_parts_mut ( data, len)
517461 }
518462}
519-
520- // Some minor tests of internal layout here. See ../integration-tests for much
521- // more.
522- #[ test]
523- fn assert_sizes ( ) {
524- use std:: mem;
525- struct EmptyWithDrop ;
526- impl Drop for EmptyWithDrop {
527- fn drop ( & mut self ) { }
528- }
529- let compiler_uses_inline_drop_flags = mem:: size_of :: < EmptyWithDrop > ( ) > 0 ;
530-
531- // Guard against accidental changes to the sizes of things.
532- assert_eq ! (
533- mem:: size_of:: <DefaultAtom >( ) ,
534- if compiler_uses_inline_drop_flags {
535- 16
536- } else {
537- 8
538- }
539- ) ;
540- assert_eq ! (
541- mem:: size_of:: <Option <DefaultAtom >>( ) ,
542- mem:: size_of:: <DefaultAtom >( ) ,
543- ) ;
544- }
0 commit comments