#proc-macro #macro #rust

macro global-id-creator-macro

A procedural macro library for generating constants

1 unstable release

0.1.0 Sep 29, 2024

#399 in Procedural macros

Download history 185/week @ 2024-09-27 21/week @ 2024-10-04 5/week @ 2024-10-11

211 downloads per month

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::create_global_id;

pub enum Global {
    Id(u8)
}

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

~240–690KB
~16K SLoC