failed_result/
windows.rs

1//! implements [`LastError`] for common types on Windows
2
3use super::*;
4use ::windows::core::HRESULT;
5use ::windows::Win32::Foundation::NTSTATUS;
6
7pub use ::windows::core::Result as WindowsResult;
8
9macro_rules! impl_int {
10    ($t:ty) => {
11        impl LastError for $t {
12            type Output = $t;
13
14            fn last_error(self) -> IoResult<$t> {
15                if self == 0 {
16                    Err(IoError::last_os_error())
17                } else {
18                    Ok(self)
19                }
20            }
21        }
22    };
23}
24
25impl_int!(u8);
26impl_int!(i32);
27impl_int!(u32);
28
29pub trait AsHResult {
30    fn as_hresult(self) -> WindowsResult<()>;
31}
32
33impl AsHResult for HRESULT {
34    fn as_hresult(self) -> WindowsResult<()> {
35        self.ok()
36    }
37}
38
39impl AsHResult for i32 {
40    fn as_hresult(self) -> WindowsResult<()> {
41        HRESULT(self).as_hresult()
42    }
43}
44
45pub trait NtStatusResult {
46    fn ntstatus_result(self) -> WindowsResult<()>;
47}
48
49impl NtStatusResult for NTSTATUS {
50    fn ntstatus_result(self) -> WindowsResult<()> {
51        self.to_hresult().ok()
52    }
53}
54
55impl NtStatusResult for i32 {
56    fn ntstatus_result(self) -> WindowsResult<()> {
57        NTSTATUS(self).ntstatus_result()
58    }
59}