Crates.io | handler_map |
lib.rs | handler_map |
version | 0.1.0 |
source | src |
created_at | 2018-09-19 19:18:35.321274 |
updated_at | 2018-09-19 19:18:35.321274 |
description | Map from types to functions that receive them |
homepage | |
repository | https://github.com/QuietMisdreavus/handler_map |
max_upload_size | |
id | 85541 |
size | 27,369 |
handler_map
like AnyMap
, but with functions instead of values
This crate began with an idle thought: "Crates like AnyMap
let you store one value of each type.
What would it take to instead store a function that took that type, like a message handler?" What
came out was this.
The basic idea is that you start with a message type, and a function that receives it by-value:
struct MyMessage;
fn handle_msg(_: MyMessage) {
println!("Got your message!");
}
Then, take one of these HandlerMap
s, and hand it the handler:
let mut map = HandlerMap::new();
map.insert(handle_msg);
This registers that type in the handler so you can call it later:
map.call(MyMessage);
// console prints "Got your message!"