GithubHelp home page GithubHelp logo

rust-phf's Introduction

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

Release 0.8.0 requires Rust 1.32.0

PHF data structures can be constucted via either the procedural macros in the phf_macros crate or code generation supported by the phf_codegen crate.

To compile the phf crate with a dependency on libcore instead of libstd, enabling use in environments where libstd will not work, set default-features = false for the dependency:

[dependencies]
# to use `phf` in `no_std` environments
phf = { version = "0.8", default-features = false }

phf_macros

use phf::phf_map;

#[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()
}
[dependencies]
phf = { version = "0.8", features = ["macros"] }

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

rust-phf's People

Contributors

sfackler avatar abonander avatar alexcrichton avatar bobo1239 avatar kmcallister avatar nox avatar simonsapin avatar aidanhs avatar chris-morgan avatar upsuper avatar techcable avatar nrc avatar mati865 avatar gz avatar sp3d avatar gwenn avatar globin avatar o01eg avatar mbrubeck avatar manishearth avatar kornelski avatar ryman avatar ignatenkobrain avatar huonw avatar erickt avatar dinfuehr avatar emberian avatar fauxfaux avatar brson avatar benjamin-l avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.