Skip to content

Commit 4837db4

Browse files
committed
Move static sets to their own module
1 parent 1a92659 commit 4837db4

File tree

3 files changed

+85
-87
lines changed

3 files changed

+85
-87
lines changed

src/atom.rs

Lines changed: 3 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![allow(non_upper_case_globals)]
1111

1212
use crate::dynamic_set::{Entry, DYNAMIC_SET};
13+
use crate::static_sets::StaticAtomSet;
1314
use debug_unreachable::debug_unreachable;
1415
use phf_shared;
1516
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -35,61 +36,6 @@ const LEN_MASK: u64 = 0xF0;
3536
const MAX_INLINE_LEN: usize = 7;
3637
const 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-
}

src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,23 @@
105105
#![crate_type = "rlib"]
106106
#![cfg_attr(test, deny(warnings))]
107107

108-
pub use crate::atom::{Atom, DefaultAtom, EmptyStaticAtomSet, PhfStrSet, StaticAtomSet};
109-
110108
mod atom;
111109
mod dynamic_set;
110+
mod static_sets;
111+
112+
pub use atom::Atom;
113+
pub use static_sets::{EmptyStaticAtomSet, PhfStrSet, StaticAtomSet};
114+
115+
/// Use this if you don’t care about static atoms.
116+
pub type DefaultAtom = Atom<EmptyStaticAtomSet>;
117+
118+
// Some minor tests of internal layout here.
119+
// See ../integration-tests for much more.
120+
121+
/// Guard against accidental changes to the sizes of things.
122+
#[test]
123+
fn assert_sizes() {
124+
use std::mem::size_of;
125+
assert_eq!(size_of::<DefaultAtom>(), 8);
126+
assert_eq!(size_of::<Option<DefaultAtom>>(), size_of::<DefaultAtom>(),);
127+
}

src/static_sets.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2014 The Servo Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
/// A static `PhfStrSet`
11+
///
12+
/// This trait is implemented by static sets of interned strings generated using
13+
/// `string_cache_codegen`, and `EmptyStaticAtomSet` for when strings will be added dynamically.
14+
///
15+
/// It is used by the methods of [`Atom`] to check if a string is present in the static set.
16+
///
17+
/// [`Atom`]: struct.Atom.html
18+
pub trait StaticAtomSet: Ord {
19+
/// Get the location of the static string set in the binary.
20+
fn get() -> &'static PhfStrSet;
21+
/// Get the index of the empty string, which is in every set and is used for `Atom::default`.
22+
fn empty_string_index() -> u32;
23+
}
24+
25+
/// A string set created using a [perfect hash function], specifically
26+
/// [Hash, Displace and Compress].
27+
///
28+
/// See the CHD document for the meaning of the struct fields.
29+
///
30+
/// [perfect hash function]: https://en.wikipedia.org/wiki/Perfect_hash_function
31+
/// [Hash, Displace and Compress]: http://cmph.sourceforge.net/papers/esa09.pdf
32+
pub struct PhfStrSet {
33+
#[doc(hidden)]
34+
pub key: u64,
35+
#[doc(hidden)]
36+
pub disps: &'static [(u32, u32)],
37+
#[doc(hidden)]
38+
pub atoms: &'static [&'static str],
39+
#[doc(hidden)]
40+
pub hashes: &'static [u32],
41+
}
42+
43+
/// An empty static atom set for when only dynamic strings will be added
44+
#[derive(PartialEq, Eq, PartialOrd, Ord)]
45+
pub struct EmptyStaticAtomSet;
46+
47+
impl StaticAtomSet for EmptyStaticAtomSet {
48+
fn get() -> &'static PhfStrSet {
49+
// The name is a lie: this set is not empty (it contains the empty string)
50+
// but that’s only to avoid divisions by zero in rust-phf.
51+
static SET: PhfStrSet = PhfStrSet {
52+
key: 0,
53+
disps: &[(0, 0)],
54+
atoms: &[""],
55+
// "" SipHash'd, and xored with u64_hash_to_u32.
56+
hashes: &[0x3ddddef3],
57+
};
58+
&SET
59+
}
60+
61+
fn empty_string_index() -> u32 {
62+
0
63+
}
64+
}

0 commit comments

Comments
 (0)