Skip to content

Commit f534a9b

Browse files
committed
More idiomatic code
1 parent 9172d26 commit f534a9b

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

string-cache-codegen/lib.rs

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,19 @@ impl AtomType {
187187
// which would cause divisions by zero in rust-phf.
188188
self.atoms.insert(String::new());
189189

190-
// Strings over 7 bytes and empty string added to static set
191-
let atoms: Vec<&str> = self
190+
// Strings over 7 bytes + empty string added to static set.
191+
// Otherwise stored inline.
192+
let (static_strs, inline_strs): (Vec<_>, Vec<_>) = self
192193
.atoms
193194
.iter()
194-
.filter(|s| s.len() > 7 || s.is_empty())
195-
.map(|s| &**s)
196-
.collect();
197-
let hash_state = phf_generator::generate_hash(&atoms);
195+
.map(String::as_str)
196+
.partition(|s| s.len() > 7 || s.is_empty());
197+
198+
// Static strings
199+
let hash_state = phf_generator::generate_hash(&static_strs);
198200
let phf_generator::HashState { key, disps, map } = hash_state;
199201
let (disps0, disps1): (Vec<_>, Vec<_>) = disps.into_iter().unzip();
200-
let atoms: Vec<&str> = map.iter().map(|&idx| atoms[idx]).collect();
202+
let atoms: Vec<&str> = map.iter().map(|&idx| static_strs[idx]).collect();
201203
let empty_string_index = atoms.iter().position(|s| s.is_empty()).unwrap() as u32;
202204
let indices = 0..atoms.len() as u32;
203205

@@ -234,45 +236,33 @@ impl AtomType {
234236
let macro_name = new_term(&*self.macro_name);
235237
let module = module.parse::<proc_macro2::TokenStream>().unwrap();
236238
let atom_prefix = format!("ATOM_{}_", type_name.to_string().to_uppercase());
237-
let const_names: Vec<_> = atoms
238-
.iter()
239-
.map(|atom| {
240-
let mut name = atom_prefix.clone();
241-
for c in atom.chars() {
242-
name.push_str(&format!("_{:02X}", c as u32))
243-
}
244-
new_term(&name)
245-
})
246-
.collect();
239+
let new_const_name = |atom: &str| {
240+
let mut name = atom_prefix.clone();
241+
for c in atom.chars() {
242+
name.push_str(&format!("_{:02X}", c as u32))
243+
}
244+
new_term(&name)
245+
};
246+
let const_names: Vec<_> = atoms.iter().copied().map(new_const_name).collect();
247247

248-
// Strings 7 bytes or less (except empty string) stored inline
249-
let short_strs: Vec<&str> = self
250-
.atoms
251-
.iter()
252-
.filter(|s| s.len() < 8 && !s.is_empty())
253-
.map(|s| &**s)
254-
.collect();
255-
let short_const_names: Vec<_> = short_strs
256-
.iter()
257-
.map(|s| {
258-
let mut name = atom_prefix.clone();
259-
for c in s.chars() {
260-
name.push_str(&format!("_{:02X}", c as u32))
261-
}
262-
new_term(&name)
263-
})
264-
.collect();
265-
let short_values: Vec<_> = short_strs
248+
// Inline strings
249+
let (inline_const_names, inline_values_and_lengths): (Vec<_>, Vec<_>) = inline_strs
266250
.iter()
267251
.map(|s| {
268-
let mut n = 0u64;
252+
let const_name = new_const_name(s);
253+
254+
let mut value = 0u64;
269255
for (index, c) in s.bytes().enumerate() {
270-
n = n | ((c as u64) << (index * 8 + 8));
256+
value = value | ((c as u64) << (index * 8 + 8));
271257
}
272-
n
258+
259+
let len = s.len() as u8;
260+
261+
(const_name, (value, len))
273262
})
274-
.collect();
275-
let short_lens: Vec<_> = short_strs.iter().map(|s| s.len() as u8).collect();
263+
.unzip();
264+
let (inline_values, inline_lengths): (Vec<_>, Vec<_>) =
265+
inline_values_and_lengths.into_iter().unzip();
276266

277267
quote! {
278268
#atom_doc
@@ -301,7 +291,7 @@ impl AtomType {
301291
pub const #const_names: #type_name = #type_name::pack_static(#indices);
302292
)*
303293
#(
304-
pub const #short_const_names: #type_name = #type_name::pack_inline(#short_values, #short_lens);
294+
pub const #inline_const_names: #type_name = #type_name::pack_inline(#inline_values, #inline_lengths);
305295
)*
306296

307297
#macro_doc
@@ -311,7 +301,7 @@ impl AtomType {
311301
(#atoms) => { #module::#const_names };
312302
)*
313303
#(
314-
(#short_strs) => { #module::#short_const_names };
304+
(#inline_strs) => { #module::#inline_const_names };
315305
)*
316306
}
317307
}

0 commit comments

Comments
 (0)