Hacker News new | ask | show | jobs
by cbrewster 2729 days ago
Great article, there aren't many resources for writing proc macros right now.

However, I think this could be done with a generic impl of WritableTemplate for all T where T: Template.

3 comments

Agreed. I was anxiously waiting for proc macros to land on stable for quite a while and was very happy when they did.

But, when the time came to implement my custom derive I had to consult many sources. I ended up piecing together what I needed from a combination of:

  - Official documentation (The Book)
  - Blog posts
  - Reading Serde code (And Syn/Proc-Macro/Proc-Macro2)
I also found the introduction of the proc-macro2 shim crate (however well intentioned) caused quite a bit of confusion. Specifically, it wasn't clean if I should use proc-macro or proc-macro2, and if I should be using the TokenStream exported by the former or the latter. Or should I be using one in some cases, and the other in some cases. Ditto for a few other things that I just can't recall right now.

I did get things put together eventually, but I don't feel I understand things well enough to explain to someone else...yet.

All that said, it's a hugely powerful feature and well worth the time if you need to do things that require intimate knowledge of the AST.

That bugged me, too -- if your entire article is about using a particular tool, then the reason for using that tool should be rock solid.

I found the linked article [1] much more helpful in explaining procedural macros, even though it didn't offer many reasons to use them (other than linking to Serde etc.).

[1] https://blog.rust-lang.org/2018/12/21/Procedural-Macros-in-R...

Yeah, I dropped out after three paragraphs thinking that the author is waaay overdoing it. If this is just a toy example, it should be labeled as such.