Compile time static maps for Rust
Clone or download
Latest commit ae4ef3e Jan 5, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci rage Jan 3, 2019
phf Release v0.7.24 Jan 5, 2019
phf_builder Release v0.7.24 Jan 5, 2019
phf_codegen Release v0.7.24 Jan 5, 2019
phf_generator Release v0.7.24 Jan 5, 2019
phf_macros Fix tests Jan 5, 2019
phf_shared Release v0.7.24 Jan 5, 2019
.gitignore Ignore IDEA files Dec 5, 2016
Cargo.toml Fix tests Dec 5, 2016
LICENSE Bump license year Jan 2, 2016
README.md Release v0.7.23 Aug 27, 2018
build_docs.sh pass args to doc builds Jan 1, 2016
release.sh Update release.sh for docs.rs Jan 16, 2017

README.md

Rust-PHF

Build Status Latest Version

Documentation

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 set the environment variable PHF_STATS it will issue a compiler note about how long it took.

Usage

PHF data structures can be constucted via either the compiler plugins in the phf_macros crate or code generation supported by the phf_codegen crate. Compiler plugins are not a stable part of Rust at the moment, so phf_macros can only be used with nightlies.

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.

phf_macros

#![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()
}

phf_codegen

build.rs

extern crate phf_codegen;

use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

fn main() {
    let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
    let mut file = BufWriter::new(File::create(&path).unwrap());

    write!(&mut file, "static KEYWORDS: phf::Map<&'static str, Keyword> = ").unwrap();
    phf_codegen::Map::new()
        .entry("loop", "Keyword::Loop")
        .entry("continue", "Keyword::Continue")
        .entry("break", "Keyword::Break")
        .entry("fn", "Keyword::Fn")
        .entry("extern", "Keyword::Extern")
        .build(&mut file)
        .unwrap();
    write!(&mut file, ";\n").unwrap();
}

lib.rs

extern crate phf;

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

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

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