I'm not sure if this is possible, but I figure I'd ask anyway!
What I want is to be able to have a manager issue commands to workers running the same binary. The manager would spin up N
worker processes, maybe read a bunch of data, then send the workers commands (through IPC or an RPC or something). The workers should ideally be "dumb" (as soon as they start, they only listen for commands and execute them).
The trouble is registering those commands on the worker processes. Since the workers shouldn't do the "read a bunch of data", the workers also don't get to the part where we send the commands to the workers (so there is no way to record the list of possible commands on the workers). Why not just add a big list at the start of main of all the possible commands? This sort of works, but A) doesn't allow crates to add their own commands, and B) requires explicitly listing out every version of a generic command (which is really weird to require registering a command from another crate just because you use it with a different generic argument).
I'd like to be able to use something like the ctor
or linkme
crates. However, every way I've tried this I run into "can't use generic parameters from outer function". My idea was something like this:
fn send_command<T: Command>(t: T) {
#[ctor::ctor]
fn register() {
some_static_map_of_dyn_command[T::name()] = Box::new(T);
}
// ... Send the data of the command to the worker.
}
This of course fails because we can't define a nested function that uses the outer generic parameter.
Is there any way to do this? It would be really unfortunate if the workers have to get smarter just because there's no way to list out the types used in send_command
.