@@ -46,13 +46,11 @@ impl AtomType {
4646 /// // Expands to: $crate::foo::FooAtom { … }
4747 /// }
4848 pub fn new ( path : & str , macro_name : & str ) -> Self {
49- let mut set = HashSet :: new ( ) ;
50- set. insert ( String :: new ( ) ) ; // rust-phf requires a non-empty set
51- assert ! ( macro_name. ends_with( '!' ) ) ;
49+ assert ! ( macro_name. ends_with( "!" ) ) ;
5250 AtomType {
5351 path : path. to_owned ( ) ,
54- macro_name : macro_name[ ..macro_name. len ( ) - 1 ] . to_owned ( ) ,
55- atoms : set ,
52+ macro_name : macro_name[ ..macro_name. len ( ) - "!" . len ( ) ] . to_owned ( ) ,
53+ atoms : HashSet :: new ( ) ,
5654 }
5755 }
5856
@@ -70,10 +68,16 @@ impl AtomType {
7068 }
7169
7270 /// Write generated code to `destination`.
73- pub fn write_to < W > ( & self , mut destination : W ) -> io:: Result < ( ) > where W : Write {
71+ pub fn write_to < W > ( & mut self , mut destination : W ) -> io:: Result < ( ) > where W : Write {
72+ // `impl Default for Atom` requires the empty string to be in the static set.
73+ // This also makes sure the set in non-empty,
74+ // which would cause divisions by zero in rust-phf.
75+ self . atoms . insert ( String :: new ( ) ) ;
76+
7477 let atoms: Vec < & str > = self . atoms . iter ( ) . map ( |s| & * * s) . collect ( ) ;
7578 let hash_state = phf_generator:: generate_hash ( & atoms) ;
7679 let atoms: Vec < & str > = hash_state. map . iter ( ) . map ( |& idx| atoms[ idx] ) . collect ( ) ;
80+ let empty_string_index = atoms. iter ( ) . position ( |s| s. is_empty ( ) ) . unwrap ( ) ;
7781
7882 let type_name = if let Some ( position) = self . path . rfind ( "::" ) {
7983 & self . path [ position + "::" . len ( ) ..]
@@ -90,12 +94,15 @@ impl AtomType {
9094 w ! ( "impl ::string_cache::StaticAtomSet for {}StaticSet {{" , type_name) ;
9195 w ! ( " fn get() -> &'static ::string_cache::PhfStrSet {{" ) ;
9296 w ! ( " static SET: ::string_cache::PhfStrSet = ::string_cache::PhfStrSet {{" ) ;
93- w ! ( " key: {:#? }," , hash_state. key) ;
97+ w ! ( " key: {}," , hash_state. key) ;
9498 w ! ( " disps: &{:?}," , hash_state. disps) ;
9599 w ! ( " atoms: &{:#?}," , atoms) ;
96100 w ! ( " }};" ) ;
97101 w ! ( " &SET" ) ;
98102 w ! ( " }}" ) ;
103+ w ! ( " fn empty_string_index() -> u32 {{" ) ;
104+ w ! ( " {}" , empty_string_index) ;
105+ w ! ( " }}" ) ;
99106 w ! ( "}}" ) ;
100107 w ! ( "#[macro_export]" ) ;
101108 w ! ( "macro_rules! {} {{" , self . macro_name) ;
@@ -114,7 +121,7 @@ impl AtomType {
114121 ///
115122 /// Typical usage:
116123 /// `.write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("foo_atom.rs"))`
117- pub fn write_to_file ( & self , path : & Path ) -> io:: Result < ( ) > {
124+ pub fn write_to_file ( & mut self , path : & Path ) -> io:: Result < ( ) > {
118125 self . write_to ( BufWriter :: new ( try!( File :: create ( path) ) ) )
119126 }
120127}
0 commit comments