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 /// 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
32pub 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)]
45pub struct EmptyStaticAtomSet;
46
47impl 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}
65