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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! implements [`LastError`] for common types on Windows

use super::*;
use ::windows::core::HRESULT;
use ::windows::Win32::Foundation::NTSTATUS;

pub use ::windows::core::Result as WindowsResult;

macro_rules! impl_int {
    ($t:ty) => {
        impl LastError for $t {
            type Output = $t;

            fn last_error(self) -> IoResult<$t> {
                if self == 0 {
                    Err(IoError::last_os_error())
                } else {
                    Ok(self)
                }
            }
        }
    };
}

impl_int!(u8);
impl_int!(i32);
impl_int!(u32);

pub trait AsHResult {
    fn as_hresult(self) -> WindowsResult<()>;
}

impl AsHResult for HRESULT {
    fn as_hresult(self) -> WindowsResult<()> {
        self.ok()
    }
}

impl AsHResult for i32 {
    fn as_hresult(self) -> WindowsResult<()> {
        HRESULT(self).as_hresult()
    }
}

pub trait NtStatusResult {
    fn ntstatus_result(self) -> WindowsResult<()>;
}

impl NtStatusResult for NTSTATUS {
    fn ntstatus_result(self) -> WindowsResult<()> {
        self.to_hresult().ok()
    }
}

impl NtStatusResult for i32 {
    fn ntstatus_result(self) -> WindowsResult<()> {
        NTSTATUS(self).ntstatus_result()
    }
}