Macro to collect metadata

This is a "pie in the sky" question. I don't think it's possible, and I don't even really know how to ask the question. But I'm hoping that someone may have an answer, or an alternative way of doing the same thing.

I have a number of structs that I wanted to define decoratively, and they each have a lot of boiler plate. So, I have a combination of a derive macro and a couple attribute macros that do the work for me.

In the end, something like this:

#[opcode = 30]
#[signature = "LOAD $D #2"]
pub struct Load {
    operand_values: OperandValues,

is expanded out to a struct that implements a couple custom traits and fleshes out a few custom methods. All that works well.

What I'd like to be able to do is -- at build or compile time -- collect information about all of these instructions and create some rust structure somewhere else after all have been expanded.

So, I would end up with (for instance) an enum in a different module with a variant for each instruction.

I have (sorta) gotten this to work with a crappy python script, but I was hoping there is a way to leverage macros. Or build scripts.

Any thoughts would be greatly appreciated. And, I can share more code if that's helpful. I just didn't want to overwhelm the topic with code that is currently working.

1 Like

Looks like there's a link-time solution, like Linkme — Rust build util // Will be interesting to hear if there's anything compile-time, though!

Sounds like crate local state.

The inventory crate could probably be used for that. It is used in the typetag crate, which could serve as inspiration.

Thank you all for the crates to look at. I'm going through them now. I'll post back what I end up doing :slight_smile:

Note that both crates are archived. I believe due to an unfixable bug.

And this one is also archived

As far as I know, there is no good solution for this problem. This is on my wishlist for rust 2027.

I really appreciate the conversation. It seems like there is no production-safe way of doing what I'm wanting, but I was able to get the GitHub - dtolnay/linkme: Safe cross-platform linker shenanigans crate working in such a way as to do what I wanted. Since the project I'm working on is really an experiment, I'm comfortable using it for now.

@MichaelV I couldn't see any documentation about why they were archived. What is the bug?

I also do not know, but expect it to be related too

I know that the crate works for my toy example, but I fear that it will explode. So I guess I should blog about my personal solution to this problem space
If I would have time ...

The linkme solution actually works really well (so far). I was even able to add the distributed_slice into the existing procedural macro. I haven't hit the reported issue, but needless to say I don't think I would want to use this for anything other than this research experiment.

For reference, this is what I'm working on: Add the most basic Assembly Language by electricjones · Pull Request #8 · electricjones/kaylee · GitHub

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.