Hi there,
This probably is a repeat question in different flavor, as I saw it asked multiple times and in multiple forums. Excuse me for that 
I'd like to pass a closure to another discrete process. Search results often end up pointing to these two crates serde_closure and serde_traitobject. I've spent some time trying out these and get the essence of heavy lifting that they cover. Before folding them into mainstream, as any crate would have its own dependencies and security aspects, wanted to check if there's a simpler way to achieve similar byte serialization of closures (sort of found in previous searches, can't find anymore).
The use case at hand is a Fn() kind, for example |message:String| println!(message)
. P.S: For data Ser-De, I'll be using SerDe.rs to allow decoupling of code and data (i.e., state).
Any useful pointers or suggestions of why-not are appreciated.
Thanks,
Kalyan
Most of the time, when closures don't have the functionality you need, you should switch to a trait. Your |message: String| println!("{message}")
could be turned into:
trait MessageConsumer {
fn consume(message: String);
}
#[derive(Serialize, Deserialize)]
struct MessagePrinter;
impl MessageConsumer for MessagePrinter {
fn consume(message: String) {
println!("{message}");
}
}
This has the advantage of working even if the processes aren't from identical binaries, like if you want separate client and server binaries, they're on different machines, or if you want to change the behavior while old serialized versions are still in use. It is also more obvious for development and debugging purposes how the whole thing works.
Right, understand that. I was just setting context about the purpose with a simple scenario. The goal is to build ability to ship functions (lambdas) to remote processes (as required), where I don't have to deliberately include variations statically in as part of local binary.