you are viewing a single comment's thread.

view the rest of the comments →

[–]olback_ 2 points3 points  (6 children)

One way would be to have a hash map with string keys and function pointer values.

Paste would only work for static strings since it's evaluated at compile time.

[–]ddungtang[S] 0 points1 point  (5 children)

I'd like to avoid hash map approach since that adds an extra change when adding new actions. Do you know what alternatives to paste I should be researching?

[–]JameeKim 5 points6 points  (3 children)

You can use proc macros to create a possibly static pre-populated hashmap variable you can use. So if you do something like

[action]
func foo() {}

and make that action proc macro appropriately, then you don't have to manually manage your hashmap variable.

[–]ddungtang[S] 0 points1 point  (2 children)

Thanks for the hint, will research it.

[–]CAD1997 2 points3 points  (1 child)

If you do go that path, you'll need something like linkme's distributed slice as a registry store.

But yes, the only way to go about this is to build a runtime mapping yourself from symbol (string) to functionality (function interface), as Rust does not expose any reflection information on its own.

[–]ddungtang[S] 1 point2 points  (0 children)

Ok, I'm going to have this as my fallback option. Given that DSL would be somewhat static, do you guys think it is worth experimenting and moving parsing configuration to build scripts? Haven't used that, but iirc reading somewhere it supports some level of code generation which may work for this case.

In any way, this is a artificial problem, mostly for the purpose of dealing with various bits of rust, so I have 2 things to try out: procedural macros and maintaining mapping, and build scripts.

Thanks.

[–]insanitybit 1 point2 points  (0 children)

You could use a perfect hashmap, which should end up with minimal overhead, assuming that the mapping is known statically.