Skip to content

zachlatta/rust-phf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

The phf/core feature will compile the phf crate with a dependency on libcore instead of libstd, enabling use in environments where libstd will not work.

Example

#![feature(plugin)]

#[plugin] #[no_link]
extern crate 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).map(|t| t.clone())
}

About

Compile time static maps for Rust

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 99.0%
  • Shell 1.0%