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.
This commit is contained in:
LGUG2Z
2021-08-06 11:16:12 -07:00
parent 88d6eee7af
commit f97cdf7c15

View File

@@ -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),
}
}
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<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),
}
}
}
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 {