Skip to content

Commit

Permalink
refactor(windows_api): gen from impls using macro
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LGUG2Z committed Aug 6, 2021
1 parent 88d6eee commit f97cdf7
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions komorebi/src/windows_api.rs
Expand Up @@ -112,32 +112,22 @@ impl From<HANDLE> for WindowsResult<HANDLE, Error> {
}
}

impl From<isize> for WindowsResult<isize, Error> {
fn from(return_value: isize) -> Self {
match return_value {
0 => Self::Err(std::io::Error::last_os_error().into()),
_ => Self::Ok(return_value),
}
}
}

impl From<u32> for WindowsResult<u32, Error> {
fn from(return_value: u32) -> 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<i32> for WindowsResult<i32, Error> {
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<T, E> From<WindowsResult<T, E>> for Result<T, E> {
fn from(result: WindowsResult<T, E>) -> Self {
Expand Down

0 comments on commit f97cdf7

Please sign in to comment.