#shared #static #time #code #maps #support

phf_shared

Support code shared by PHF libraries

59 releases

0.7.23 Aug 27, 2018
0.7.22 Apr 28, 2018
0.7.21 Jan 19, 2017
0.7.20 Nov 23, 2016
0.1.0 Nov 27, 2014

#4 in Date and time

Download history 17351/week @ 2018-06-04 21817/week @ 2018-06-11 19650/week @ 2018-06-18 21421/week @ 2018-06-25 24187/week @ 2018-07-02 19487/week @ 2018-07-09 22599/week @ 2018-07-16 23700/week @ 2018-07-23 20461/week @ 2018-07-30 23517/week @ 2018-08-06 21950/week @ 2018-08-13 21343/week @ 2018-08-20 19302/week @ 2018-08-27

92,260 downloads per month
Used in 977 crates (7 directly)

MIT license

6KB
164 lines

Rust-PHF

CircleCI 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 when compiling with optimizations.

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.

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()
}

Dependencies

~40KB