Skip to content

Latest commit

 

History

History
48 lines (38 loc) · 1.3 KB

README.md

File metadata and controls

48 lines (38 loc) · 1.3 KB

Rust-PHF

Build Status

Rust-PHF is a library to generate efficient lookup tables at compile time using perfect hash functions.

It currently uses the CHD algorithm and can generate a 100,000 entry map in roughly .4 seconds. By default statistics are not produced, but if you use the phf_mac crate with the stats feature enabled (writing phf_macros/stats in the [dependencies] section of your Cargo.toml instead of phf_macros) and set the environment variable PHF_STATS it will issue a compiler note about how long it took.

Documentation is available at https://sfackler.github.io/rust-phf/doc/phf

Example

#![feature(plugin)]
#![plugin(phf_macros)]

extern crate phf;

#[derive(Clone)]
pub enum Keyword {
    Loop,
    Continue,
    Break,
    Fn,
    Extern,
}

static KEYWORDS: phf::Map<&'static str, Keyword> = phf_map! {
    "loop" => Keyword::Loop,
    "continue" => Keyword::Continue,
    "break" => Keyword::Break,
    "fn" => Keyword::Fn,
    "extern" => Keyword::Extern,
};

pub fn parse_keyword(keyword: &str) -> Option<Keyword> {
    KEYWORDS.get(keyword).cloned()
}