From e121e6585944b44a66cff4903ceb4d830be39fd2 Mon Sep 17 00:00:00 2001 From: Matt Bilker Date: Fri, 17 Sep 2021 09:14:54 +0000 Subject: [PATCH] Move main macro body one indentation level out --- src/lib.rs | 93 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2d24a28..bbd0530 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -314,61 +314,66 @@ fn apply_profile_override(config: &mut VgpuConfig) -> bool { let gpu_type = config.gpu_type; macro_rules! handle_copy_overrides { + ($field:ident) => { + if let Some(value) = config_override.$field { + info!( + "Patching nvidia-{}/{}: {} -> {}", + gpu_type, + stringify!($field), + config.$field, + value + ); + + config.$field = value; + } + }; ($($field:ident),*$(,)?) => { $( - if let Some(value) = config_override.$field { - info!( - "Patching nvidia-{}/{}: {} -> {}", - gpu_type, - stringify!($field), - config.$field, - value - ); - - config.$field = value; - } + handle_copy_overrides!($field); )* }; } macro_rules! handle_str_overrides { + ($field:ident) => { + if let Some(value) = config_override.$field { + let value_bytes = value.as_bytes(); + + // Use `len - 1` to account for the required NULL terminator. + if value_bytes.len() > config.$field.len() - 1 { + error!( + "Patching nvidia-{}/{}: value '{}' is too long", + gpu_type, + stringify!($field), + value + ); + + return false; + } else { + info!( + "Patching nvidia-{}/{}: '{}' -> '{}'", + gpu_type, + stringify!($field), + from_c_str(&config.$field), + value + ); + + // Zero out the field first. + config.$field.fill(0); + + // Write the string bytes. + let _ = config.$field[..].as_mut().write_all(value_bytes); + } + } + }; ($($field:ident),*$(,)?) => { $( - if let Some(value) = config_override.$field { - let value_bytes = value.as_bytes(); - - // Use `len - 1` to account for the required NULL terminator. - if value_bytes.len() > config.$field.len() - 1 { - error!( - "Patching nvidia-{}/{}: value '{}' is too long", - gpu_type, - stringify!($field), - value - ); - - return false; - } else { - info!( - "Patching nvidia-{}/{}: '{}' -> '{}'", - gpu_type, - stringify!($field), - from_c_str(&config.$field), - value - ); - - // Zero out the field first - config.$field.fill(0); - - let _ = config.$field[..].as_mut().write_all(value_bytes); - } - } + handle_str_overrides!($field); )* - } + }; } - /* - * While the following could be done with two statements. I wanted the log statements to be in - * field order. - */ + // While the following could be done with two statements. I wanted the log statements to be in + // field order. handle_copy_overrides! { gpu_type,