macrotk-derive

Crates.iomacrotk-derive
lib.rsmacrotk-derive
version0.2.0
sourcesrc
created_at2021-05-13 19:35:43.490844
updated_at2021-05-15 17:32:00.801575
descriptionmacrotk core derive macros.
homepagehttps://github.com/frostu8/macrotk
repositoryhttps://github.com/frostu8/macrotk.git
max_upload_size
id397080
size5,818
(frostu8)

documentation

https://docs.rs/macrotk-derive

README

macrotk

An extensible macro toolkit for Rust.

I got tired of writing the same functions for handling parameters to macros, specifically attribute macros. It turns out that I was so right about being tired of it that you can just write a macro for it. Yep, now we're going full macroception.

This has actually already been done before, but it's old, and I wanted to try out writing something like it.

use syn::{parse_macro_input, LitStr};

use macrotk::{meta::Meta, FromMeta};

use proc_macro::TokenStream;

#[derive(FromMeta)]
struct MacroMeta {
    something: LitStr,
    otherthing: LitStr,
}

#[proc_macro_attribute]
pub fn cool_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
    let attr = parse_macro_input!(attr as Meta<MacroMeta>);

    // now we can just use these fields!
    let something = &attr.something;
    let otherthing = &attr.something;
    
    // ... do stuff ...

    item
}
Commit count: 16

cargo fmt