string_cache/static_sets.rs
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
18pub trait StaticAtomSet: Ord {
19 /// Get the location of the static string set in the binary.
20 fn get() -> &'static PhfStrSet;
21}
22
23/// A string set created using a [perfect hash function], specifically
24/// [Hash, Displace and Compress].
25///
26/// See the CHD document for the meaning of the struct fields.
27///
28/// [perfect hash function]: https://en.wikipedia.org/wiki/Perfect_hash_function
29/// [Hash, Displace and Compress]: http://cmph.sourceforge.net/papers/esa09.pdf
30pub struct PhfStrSet {
31 #[doc(hidden)]
32 pub key: u64,
33 #[doc(hidden)]
34 pub disps: &'static [(u32, u32)],
35 #[doc(hidden)]
36 pub atoms: &'static [&'static str],
37 #[doc(hidden)]
38 pub hashes: &'static [u64],
39}
40
41/// An empty static atom set for when only dynamic strings will be added
42#[derive(PartialEq, Eq, PartialOrd, Ord)]
43pub struct EmptyStaticAtomSet;
44
45impl StaticAtomSet for EmptyStaticAtomSet {
46 fn get() -> &'static PhfStrSet {
47 // The name is a lie: this set is not empty (it contains the empty string)
48 // but that’s only to avoid divisions by zero in rust-phf.
49 static SET: PhfStrSet = PhfStrSet {
50 key: 0,
51 disps: &[(0, 0)],
52 atoms: &[""],
53 // "" SipHash'd, and xored with u64_hash_to_u32.
54 hashes: &[0x3ddddef3],
55 };
56 &SET
57 }
58}