Hi there!
I want to use the HashMap
object to collect callable objects. So I can call the callable objects dynamically.
Code below:
-
main
function:
fn main() {
let mut id = ID::new();
let mut machine = Machine::new("Rust", id.gen());
machine.print();
machine.add_operation("test", || { println!("Hello, machine"); });
let test = machine.get_operation("test").unwrap();
test();
}
-
Machine
structure:
struct Machine {
name: String,
id: u32,
operations: HashMap<String, Arc<dyn Fn()>>,
}
impl Machine {
fn new<T: Into<String>>(name: T, id: u32) -> Self {
Self {
name: name.into(),
id,
operations: HashMap::new(),
}
}
fn print(&self) {
println!("Machine<name: {}, id: {}>", self.name, self.id);
}
fn add_operation<F, T>(&mut self, name: T, f: F)
where
F: Fn() + 'static,
T: Into<String>,
{
self.operations.insert(name.into(), Arc::new(f));
}
fn get_operation<T: Into<String>>(&self, name: T) -> Result<Arc<dyn Fn()>, &'static str> {
let key = name.into();
if let Some(op) = self.operations.get(&key) {
Ok(Arc::clone(op))
} else {
Err("Unknown operation")
}
}
}
-
ID
structure:
struct ID(u32);
impl ID {
fn new() -> Self {
Self(0)
}
fn gen(&mut self) -> u32 {
self.0 += 1;
self.0
}
}
Now, I want to know how to store a method of Machine
into operations
?
// How could I add a method of struct into a hashmap belong the struct?
machine.add_operation("print", || { machine.print(); });
let print = machine.get_operation("print").unwrap();
print();
Thanks in advance.