Skip to content

What we are doing?

For example below is a list of things which don't take much time to implement and can be fun to achieve and learn with.

  • [-] Write a rust proc macro which takes away or adds some fields to a struct.
    • Remove all fields from a struct/enum. (Derive Macro)
    • Remove all String type fields from a struct. (Derive Macro)
    • Wrap all the type wrapped in Option if there is a #[option] attribute on the field. (Derive attribute macro)

Setting up a basic setup

We create a library crate with cargo new --lib break_macro. To tell rust tooling that we are making a proc macro we have to add the below line to Cargo.toml

yaml
[lib]
proc-macro = true

Oh and if you tried to write tests in the same file as the proc macro ( src/lib.rs in our case ) you might have already gotten a error saying that you can't use the macro. Macro can't be used in the same crate they are defined in.
So test it we will have to use the tests folder in rust.

I would recommend that you read the below chapter from rust lang docs. Procedural Macros - The Rust Reference


Clear macro

You can check out the clear macro code at Rust Fun Series part 1 Macros Clear Macro

Looking at the input (DeriveInput struct struture) we can see that its input.data field contains all the fields.

rust
pub struct DeriveInput {
    pub attrs: Vec<Attribute>,
    pub vis: Visibility,
    pub ident: Ident,
    pub generics: Generics,
    pub data: Data,
}

Looking at the Data type of the struct it is a enum with 3 possible values.

rust
pub enum Data {
    Struct(DataStruct),
    Enum(DataEnum),
    Union(DataUnion),
}

Looking at DataStruct we can easily see that all the fields are contained in the fields property so we just have to make the fields empty. Similarly it can be done for the enum but you will have to look which field to make empty yourself.


Clear String macro

You can check out the clear String macro code at Rust Fun Series part 1 Macros Clear String Macro


Extra


Proc macros are a really fun and interesting topic related to rust.

Did you know there that (looking at the options from rustc --crate-type flag) there are 6 different type of compilation modes in which the rust compiler can output a file in.

bin, dylib, lib, proc-macro, rlib, staticlib

Each of the above binary types are a huge topic on there own but you should explore them on there own.
There is also a hint here (looking at you rlib) from which you can figure out some of the working of cargo and how it manages your project.