Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile-time lookup tables via syntax extensions #4864

Closed
thestinger opened this issue Feb 10, 2013 · 4 comments
Closed

compile-time lookup tables via syntax extensions #4864

thestinger opened this issue Feb 10, 2013 · 4 comments
Labels
A-syntaxext Area: Syntax extensions

Comments

@thestinger
Copy link
Contributor

It would be awesome to support building constant lookup tables at compile-time similar to gperf. This is a really common pattern and could potentially be a big performance win in many places (I'm sure rustc has a lot of constant tables).

A really simple proof of concept would sort an array and provide an object implementing the core::container::Map interface (once MutableMap is split out) via binary search.

The naming would be something along the lines of map!, set!, ordered_map! and ordered_set! along with the corresponding types ConstantMap, ConstantSet, ConstantOrderedMap, ConstantOrderedSet.

@graydon
Copy link
Contributor

graydon commented Apr 30, 2013

Agreed, but I think this would make a better 3rd party library (once syntax extensions are independently loadable) rather than necessarily part of the main compiler. I'll leave it open here since we don't have a syntax-extensions-wanted page yet.

@mstewartgallus
Copy link
Contributor

A functional manner of doing this would be to use a match expression such as:

fn lookup_table(key: &str) -> Option<uint> {
    match key {
        "foo" => Some(0),
        "bar" => Some(1),
        "car" => Some(2),
        _ => None
    }
}

A macro could easily be created that would provide a nicer syntax for this. Is there any reason that a hash table could not be used to implement big enough to benefit match expressions?

@sfackler
Copy link
Member

I'm working on this: https://github.com/sfackler/rust-mphf

@sfackler
Copy link
Member

I'm going to close this out as there's a decently mature implementation at https://github.com/sfackler/rust-phf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions
Projects
None yet
Development

No branches or pull requests

4 participants