Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a macro to generate compile-time unique integers?

I need several parts of of program, in different modules, to have a unique integer.

e.g.

pub fn foo() -> uint64_t {
    unique_integer!();
}

pub fn bar() -> uint64_t {
    unique_integer!();
}

(foo() should never return the same as bar(), but the values themselves are meaningless and do not need to be stable across builds. All invocations of foo() must return the same values, as must all invocations to bar(). It is preferred, but not essential, that the values are contiguous.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way of using a macro to do this?

>Solution :

It’s possible to do something like this with once_cell, using a static atomic variable as a counter:

use core::sync::atomic::{Ordering, AtomicU64};
use once_cell::sync::Lazy;

static COUNTER: AtomicU64 = AtomicU64::new(0);

fn foo() -> u64 {
    static LOCAL_COUNTER: Lazy<u64> = Lazy::new(|| COUNTER.fetch_add(1, Ordering::Relaxed));
    *LOCAL_COUNTER
}

fn bar() -> u64 {
    static LOCAL_COUNTER: Lazy<u64> = Lazy::new(|| COUNTER.fetch_add(1, Ordering::Relaxed));
    *LOCAL_COUNTER
}

fn main() {
    dbg!(foo()); // 0
    dbg!(foo()); // still 0
    dbg!(bar()); // 1
    dbg!(foo()); // unchanged - 0
    dbg!(bar()); // unchanged - 1
}

Playground

And, yes, the repeating code can be, as usual, wrapped in macro:

macro_rules! unique_integer {
    () => {{
        static LOCAL_COUNTER: Lazy<u64> = Lazy::new(|| COUNTER.fetch_add(1, Ordering::Relaxed));
        *LOCAL_COUNTER
    }}
}

fn foo() -> u64 {
    unique_integer!()
}

fn bar() -> u64 {
    unique_integer!()
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading