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

Suggestion: lookup table construction with const fn #188

Open
arucil opened this issue Feb 10, 2020 · 4 comments
Open

Suggestion: lookup table construction with const fn #188

arucil opened this issue Feb 10, 2020 · 4 comments
Labels
blocked-on-upstream the progress is blocked on upstream

Comments

@arucil
Copy link

arucil commented Feb 10, 2020

Currently lookup tables are contructed with macros, where keys and values are almost literals. With constant functions, one can use a rich set of builtin constant expressions during the construction of lookup tables. For example,

const A: &str = "aaa";
const B: &str = "bbb";
const TABLE: Map<&'static str, i32> = phf_map(&[
  (A, 1 * 3 * 5),
  (B, [1,2,3,4].len() as i32),
]);
@pickfire
Copy link
Contributor

pickfire commented Aug 1, 2020

But I wonder if there is any way to manually construct the table, I saw a place where we have a use case to build a two way Map, I wonder if it could be done without duplicating all the stuff.

pub(crate) struct NamedColorMap {
    name_to_rgba: HashMap<&'static str, [u8; 4]>,
    rgba_to_name: HashMap<[u8; 4], &'static str>,
}

@abonander
Copy link
Collaborator

This is theoretically possible, however constifying the map generator code isn't trivial. Have a look. A glance there's a lot of code here that depends on non-const APIs:

  • random number generation (albeit using a PRNG with a fixed seed which can be constified)
  • dynamic allocation with vec![], but with theoretically fixed upper bounds that could be replaced by arrays.
  • iteration (but could be replaced by looping with incrementing indices, which are allowed in const)
  • invocations of the PhfHash trait (via phf_shared::hash)
  • sorting (which requires a trait or closure but could be constified)

It's the trait-based hashing that would be the most difficult to replace right now because we don't have const trait fns. Constifying this would probably require hand-monomorphized code and a different constructor function for each possible key type (deduplicated with macros, sure, but that's suboptimal).

@abonander
Copy link
Collaborator

rust-lang/rfcs#2632 would likely allow this, assuming some trait impls get constified as well.

@JohnTitor JohnTitor added the blocked-on-upstream the progress is blocked on upstream label Jun 17, 2021
@ImUrX
Copy link

ImUrX commented Sep 20, 2023

I dont think that a similar RFC is gonna ever come out and be merged in within a year. Maybe #242 is a good step on just making it work with the current hacks that Rust permits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked-on-upstream the progress is blocked on upstream
Projects
None yet
Development

No branches or pull requests

5 participants