5

Is it possible to create a macro which counts the number of expanded items?

macro_rules! count {
    ($($name:ident),*) => {
        pub enum Count {
           $(
               $name = 1 << $i // $i is the current expansion index
            ),*
        }
    }
}

count!(A, B, C);
malbarbo
  • 10,717
  • 1
  • 42
  • 57
scooterman
  • 1,336
  • 2
  • 17
  • 36
  • Does this answer your question? [Counting length of repetition in macro](https://stackoverflow.com/questions/34304593/counting-length-of-repetition-in-macro) – Tarun Aditya Jun 14 '22 at 12:30

4 Answers4

6

Here is a macro that counts the number of matched items:

macro_rules! count_items {
    ($name:ident) => { 1 };
    ($first:ident, $($rest:ident),*) => {
        1 + count_items!($($rest),*)
    }
}

fn main() {
    const X: usize = count_items!(a);
    const Y: usize = count_items!(a, b);
    const Z: usize = count_items!(a, b, c);
    assert_eq!(1, X);
    assert_eq!(2, Y);
    assert_eq!(3, Z);
}

Note that the counting is computed at compile time.


For your example, you can do it using accumulation:

macro_rules! count {
    ($first:ident, $($rest:ident),*) => (
        count!($($rest),+ ; 0; $first = 0)
    );
    ($cur:ident, $($rest:ident),* ; $last_index: expr ; $($var:ident = $index:expr)+) => (
        count!($($rest),* ; $last_index + 1; $($var = $index)* $cur = $last_index + 1)
    );
    ($cur:ident; $last_index:expr ; $($var:ident = $index:expr)+) => (
        #[repr(C)]
        enum Count {
            $($var = 1 << $index),*,
            $cur = 1 << ($last_index + 1),
        }
    );
}

pub fn main() {
    count!(A, B, C, D);
    assert_eq!(1, Count::A as usize);
    assert_eq!(2, Count::B as usize);
    assert_eq!(4, Count::C as usize);
    assert_eq!(8, Count::D as usize);
}
malbarbo
  • 10,717
  • 1
  • 42
  • 57
1

Yes, if you pack it as array of idents:

macro_rules! count {
    ($($name:ident),*) => {
        {
            let counter = [$(stringify!($name),)*];
            counter.len()
        }
    }
}

Count, names, reverse order of names are available. After, you can use it to construct something. For enum building you have to join it with something like this.

DenisKolodin
  • 13,501
  • 3
  • 62
  • 65
1

Since this question is general, posting an example of counting where arguments are separated by white-space (not commas).

Although in retrospect it seems obvious, it took me a while to figure out:

/// Separated by white-space.
#[macro_export]
macro_rules! count_args_space {
    ($name:ident) => { 1 };
    ($first:ident $($rest:ident) *) => {
        1 + count_args_space!($($rest) *)
    }
}

/// Separated by commas.
#[macro_export]
macro_rules! count_args_comma {
    ($name:ident) => { 1 };
    ($first:ident, $($rest:ident),*) => {
        1 + count_args_comma!($($rest),*)
    }
}

Second example is from @malbarbo, just posting to so you can see the 2x changes that were needed.

ideasman42
  • 42,413
  • 44
  • 197
  • 320
0

In this context, no. A macro could create an expression that counts the number of identifiers passed to it, but it would only be evaluated at runtime. I created this example in just a few minutes, but I realized it would not work for what you're doing.

Compiler plugins, however, are particularly suited to this sort of work. While they're not trivial to implement, I don't think it would be overly difficult to create one for this purpose. Maybe take a look, try your hand at it, and come back if you get stuck?

Austin B
  • 1,590
  • 1
  • 13
  • 25
  • You can count in enum context too, but unfortunately one enum variant as usize is not a valid constant expression for another. `enum Count { $( $name ),* }` – bluss May 10 '15 at 21:44