mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-07-15 09:32:52 +02:00
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:
@@ -112,32 +112,22 @@ impl From<HANDLE> for WindowsResult<HANDLE, Error> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<isize> for WindowsResult<isize, Error> {
|
macro_rules! impl_from_integer_for_windows_result {
|
||||||
fn from(return_value: isize) -> Self {
|
( $( $integer_type:ty ),+ ) => {
|
||||||
|
$(
|
||||||
|
impl From<$integer_type> for WindowsResult<$integer_type, Error> {
|
||||||
|
fn from(return_value: $integer_type) -> Self {
|
||||||
match return_value {
|
match return_value {
|
||||||
0 => Self::Err(std::io::Error::last_os_error().into()),
|
0 => Self::Err(std::io::Error::last_os_error().into()),
|
||||||
_ => Self::Ok(return_value),
|
_ => Self::Ok(return_value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
)+
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
impl From<u32> for WindowsResult<u32, Error> {
|
impl_from_integer_for_windows_result!(isize, u32, i32);
|
||||||
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<T, E> From<WindowsResult<T, E>> for Result<T, E> {
|
impl<T, E> From<WindowsResult<T, E>> for Result<T, E> {
|
||||||
fn from(result: WindowsResult<T, E>) -> Self {
|
fn from(result: WindowsResult<T, E>) -> Self {
|
||||||
|
|||||||
Reference in New Issue
Block a user