#proc-macro #macro #rust

macro global-id-creator-macro

A procedural macro library for generating constants

2 releases

new 0.1.1 Nov 17, 2024
0.1.0 Sep 29, 2024

#409 in Procedural macros

MIT license

5KB

GLOBAL ID CREATOR

this is a small procedural macro which can be use in global to create a list of constant with the id is u8. increace by 1 for each constant created

use global_id_creator_macro::ntl_create_global_id;

pub enum Global {
    Id(u8)
}

ntl_create_global_id!(Global, CONST_A, CONST_B, CONST_C, CONST_D, CONST_E);

fn main() {
    println!("CONST_A = {:?}", CONST_A);
    println!("CONST_B = {:?}", CONST_B);
    println!("CONST_C = {:?}", CONST_C);
    println!("CONST_D = {:?}", CONST_D);
    println!("CONST_E = {:?}", CONST_E);
}

output:

CONST_A = Id(100)
CONST_B = Id(101)
CONST_C = Id(102)
CONST_D = Id(103)
CONST_E = Id(104)

the macro will be expanded like this:

pub const CONST_A: Global = Global::Id(100);
pub const CONST_A: Global = Global::Id(101);
pub const CONST_A: Global = Global::Id(102);
pub const CONST_A: Global = Global::Id(103);
pub const CONST_A: Global = Global::Id(104);

This crate is just use for my personal project, so it maybe not fit which your need

Dependencies

~225–670KB
~16K SLoC