From dce5b8d8832afea53317ad6b9d5a67ec2972b82e Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 20 Apr 2023 11:25:11 +0200 Subject: [PATCH] Adds a cleanup for the macro --- src/main.rs | 52 ++++++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 91e7076..a184f05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,27 +29,29 @@ struct Args { } macro_rules! list_actions { - ($($id1:literal , $proj1:ident, $action1:ident),+ ) => { - - - vec![ - $(( - $id1, - - Box::new(move || { - match $proj1.lock().unwrap().$action1() { - Ok(_) => {} - Err(e) => { - println!( - "[ERROR] An error occurend while sending a ({}) message", - stringify!($action1) - ); - println!(" {}", e) - } - }; - }), - ),)+ - ] + ($proj:ident, $($id:literal, $action:ident),+ ) => { + { + let mut temp_vec:Vec<(u8, Box<(dyn Fn() + Send + 'static)>)> = Vec::new(); + $( + let proj = Arc::clone(&$proj); + temp_vec.push(( + $id, + Box::new(move || { + match proj.lock().unwrap().$action() { + Ok(_) => {} + Err(e) => { + println!( + "[ERROR] An error occurend while sending a ({}) message", + stringify!($action) + ); + println!(" {}", e) + } + }; + }), + )); + )+ + temp_vec + } }; } @@ -84,12 +86,6 @@ fn main() { "admin1", "panasonic", ))); - let proj1 = Arc::clone(&proj); - let proj2 = Arc::clone(&proj); - - // proj.lock().unwrap().shutter_on().unwrap(); - // proj.lock().unwrap().shutter_off().unwrap(); - // proj.lock().unwrap().get_osd().unwrap(); let mut output_interface = create_output_interface(args.midi_output, "Rusty MIDI Commander Output"); @@ -98,6 +94,6 @@ fn main() { args.midi_input.unwrap(), "Rusty MIDI Commander Input", &mut output_interface, - list_actions!(3, proj1, shutter_on, 4, proj2, shutter_off), + list_actions!(proj, 3, shutter_on, 4, shutter_off), ); }