1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#![cfg(feature = "f_Win32_Foundation")]

use crate::windows;
use windows::Win32::Foundation::{E_FAIL, LPARAM};

pub trait BoolExt {
    /// Like [`BOOL::ok()`](windows::Win32::Foundation::BOOL::ok), but returning an `Error` with [`HRESULT`](windows::core::HRESULT) [`E_FAIL`](windows::Win32::Foundation::E_FAIL) instead of calling `GetLastError()`.
    fn ok_or_e_fail(self) -> windows::core::Result<()>;
}

impl BoolExt for windows::Win32::Foundation::BOOL {
    fn ok_or_e_fail(self) -> windows::core::Result<()> {
        if self.as_bool() {
            Ok(())
        } else {
            Err(E_FAIL.into())
        }
    }
}

pub trait LParamExt {
    unsafe fn cast_to_ref<T>(&self) -> &T;
    unsafe fn cast_to_mut<T>(&mut self) -> &mut T;
}

impl LParamExt for LPARAM {
    unsafe fn cast_to_ref<T>(&self) -> &T {
        &*(self.0 as *const T)
    }

    unsafe fn cast_to_mut<T>(&mut self) -> &mut T {
        &mut *(self.0 as *mut T)
    }
}