emproof-com / nyxstone

Nyxstone: assembly / disassembly library based on LLVM, implemented in C++ with Rust and Python bindings, maintained by emproof.com
https://www.emproof.com
MIT License
230 stars 6 forks source link

rust: `assemble` generics forces caller to create two containers #48

Closed stuxnot closed 2 weeks ago

stuxnot commented 2 months ago

When implementing a small cli program using the rust bindings, I noticed that the generics in the assemble functions force the caller to create a HashMap<&str, u64>, which is only possible when also owning an additional container holding the Strings of the labels. When dynamically creating the labels, this forces the caller to have an additional container holding the actual Strings.

My proposal is changing the generics of the assemble functions to take AsRef<str> instead of &str. This way both Map<&str, _> and &Map<String, _> could be passed to the functions. The only downside to this change would be that rust will be unable to infer the type of an empty map, forcing the caller to specify the type of the map explicitly:

nyxstone.assemble(&assembly, 0, &HashMap::<&str, _>::new());
stuxnot commented 2 months ago

The downside of having to specify the type of the empty map could also be removed by having an additional function which has no labels argument, since the caller already knows that he will not need labels if he specified an empty map.