From f97cdf7c153da6a70cb43af4aeb0412bdfee60fd Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 6 Aug 2021 11:16:12 -0700 Subject: [PATCH] refactor(windows_api): gen from impls using macro My first time writing a macro. Figured I could clean up the repetition a little by using a macro to generate From impls for WindowsResult on the primative integer types that various windows-rs functions return. --- komorebi/src/windows_api.rs | 38 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 78fec8d2..16f60416 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -112,32 +112,22 @@ impl From for WindowsResult { } } -impl From for WindowsResult { - fn from(return_value: isize) -> Self { - match return_value { - 0 => Self::Err(std::io::Error::last_os_error().into()), - _ => Self::Ok(return_value), - } - } +macro_rules! impl_from_integer_for_windows_result { + ( $( $integer_type:ty ),+ ) => { + $( + impl From<$integer_type> for WindowsResult<$integer_type, Error> { + fn from(return_value: $integer_type) -> Self { + match return_value { + 0 => Self::Err(std::io::Error::last_os_error().into()), + _ => Self::Ok(return_value), + } + } + } + )+ + }; } -impl From for WindowsResult { - fn from(return_value: u32) -> Self { - match return_value { - 0 => Self::Err(std::io::Error::last_os_error().into()), - _ => Self::Ok(return_value), - } - } -} - -impl From for WindowsResult { - fn from(return_value: i32) -> Self { - match return_value { - 0 => Self::Err(std::io::Error::last_os_error().into()), - _ => Self::Ok(return_value), - } - } -} +impl_from_integer_for_windows_result!(isize, u32, i32); impl From> for Result { fn from(result: WindowsResult) -> Self {