Dumping crate constants

Hi all

I am trying to invent a way to reference some constants / ids declared in Rust crate in Python scripts. Rust constants are declared as:

const SHIFT:usize = 16;
const ID1:u32 = 10 << SHIFT;

Ideally, I need a tool to dump constant identifiers and their computed values into stdout :).
Is there some magic command (cargo/rustc/whatever) to help with that?


Just speculating here, but I think you’d need to pull it from the debug information.

What I’ve done in the past when I’ve needed to do this type of thing is to write a small program which generates the constant names and values as output, using old fashioned print statements. In your case, I would have it print some python source code.

You could use syn to parse the Rust source code into an AST … playground

But you’ll have to evaluate any expressions to get the computed value.

Parsing source code should be fine with your example, but how to evaluate them?
As for debug information - any hints how to do it?

So far grepping / massaging in sed and evaluating constants in Python still seems like the easiest approach :slight_smile:

As for debug info, I’d start with seeing if gdb can report on the value of the constant. If it can then it becomes an exercise in figuring out how gdb parses the debug info. I suspect that there are helper libraries etc, but not sure how much effort would be required to do that.