windows_helpers/
foundation.rs

1#![cfg(feature = "f_Win32_Foundation")]
2
3use crate::windows;
4use windows::Win32::Foundation::{E_FAIL, LPARAM};
5
6pub trait BoolExt {
7    /// 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()`.
8    fn ok_or_e_fail(self) -> windows::core::Result<()>;
9}
10
11impl BoolExt for windows::Win32::Foundation::BOOL {
12    fn ok_or_e_fail(self) -> windows::core::Result<()> {
13        if self.as_bool() {
14            Ok(())
15        } else {
16            Err(E_FAIL.into())
17        }
18    }
19}
20
21pub trait LParamExt {
22    unsafe fn cast_to_ref<T>(&self) -> &T;
23    unsafe fn cast_to_mut<T>(&mut self) -> &mut T;
24}
25
26impl LParamExt for LPARAM {
27    unsafe fn cast_to_ref<T>(&self) -> &T {
28        &*(self.0 as *const T)
29    }
30
31    unsafe fn cast_to_mut<T>(&mut self) -> &mut T {
32        &mut *(self.0 as *mut T)
33    }
34}