[SOLVED]At compile time, How to collect some functions and call them?

hello everybody,
I'm new here and in Rust, and I'm trying to write a little game in Rust.

In my situation, I got a lot of initializing functions to call, like:

  • room manager: init_room_mgr,
  • player manager: init_player_mgr,
  • and etc.

as there are too many things like these in games, and they varies a lot, it is not rational to always insert new lines into my bootstrap code like main.rs.

so I've been expecting something like a macro, to mark those init funcs and then call them at once, like:

// in mod1.rs
#something_I_wanted(stage1)
pub fn init_fn1(){
....
}

// in mod2.rs
#something_I_wanted(stage1)
pub fn init_fn2(){
....
}

// in mod3.rs
#something_I_wanted(stage2)
pub fn init_fn3(){
....
}

// in main.rs
pub fn main(){
something_to_call_functions_for(stage1);
something_to_call_functions_for(stage2);
}

But I don't know how to code that thing exactly, even after I read the macro section in the book.
so, if there is something I can use from crates.io, or any one can give me a simple example for such a macro?

thanks!

Something like inventory?

1 Like

aha! that's it! and i found the ctor crate which is its basement.
as ctor has some limits, i'd prefer inventory, though i'm not sure if inventory handled this issue or not.
thank you so much!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.