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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::*;
use bytemuck::*;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct HResultFacilityMicrosoft(pub(crate) u16);
impl HResultFacilityMicrosoft {
#[doc(hidden)] pub const fn from_constant(value: u16) -> Self { assert!(value <= 0xFFF, "HRESULT facilities are only 12 bits"); Self(value) }
pub const fn to_u16(self) -> u16 { self.0 }
pub const fn to_u32(self) -> u32 { self.0 as _ }
}
impl From<HResultFacilityMicrosoft> for u16 { fn from(f: HResultFacilityMicrosoft) -> Self { f.0 } }
impl From<HResultFacilityMicrosoft> for u32 { fn from(f: HResultFacilityMicrosoft) -> Self { f.0.into() } }
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Zeroable)] #[repr(transparent)] pub struct HResultSuccess(pub(crate) u32);
impl HResultSuccess {
#[doc(hidden)] pub const fn from_constant(value: u32) -> Self { assert!(value & 0x8000_0000 == 0, "HResultSuccess::from_constant: HRESULT is an error (high bit set)"); Self(value) }
pub const fn is_customer(self) -> bool { self.0 & 0x20000000 != 0 }
pub const fn is_ntstatus(self) -> bool { self.0 & 0x10000000 != 0 }
pub const fn facility (self) -> u16 { (self.0 & 0x0FFF0000 >> 16) as _ }
pub const fn code (self) -> u16 { self.0 as _ }
pub const fn to_u32 (self) -> u32 { self.0 }
}
impl From<HResultSuccess> for u32 { fn from(hr: HResultSuccess) -> Self { hr.0 } }
impl From<HResultSuccess> for i32 { fn from(hr: HResultSuccess) -> Self { hr.0 as _ } } #[allow(useless_deprecated)]
#[deprecated = "allows the construction of 'successful' hresults from error values"]
impl From<u32> for HResultSuccess { fn from(hr: u32) -> Self { Self(hr) } }
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] pub struct HResultError(pub(crate) u32);
impl HResultError {
pub const fn from_win32(value: ErrorCode) -> Self { Self(0x80070000 | (value.0 as u32)) }
#[doc(hidden)] pub const fn from_constant(value: u32) -> Self { assert!(value & 0x8000_0000 != 0, "HResultError::from_constant: HRESULT is a success (high bit not set)"); Self(value) }
pub const fn is_customer(self) -> bool { self.0 & 0x20000000 != 0 }
pub const fn is_ntstatus(self) -> bool { self.0 & 0x10000000 != 0 }
pub const fn facility (self) -> u16 { (self.0 & 0x0FFF0000 >> 16) as _ }
pub const fn code (self) -> u16 { self.0 as _ }
pub const fn to_u32 (self) -> u32 { self.0 }
}
impl From<HResultError> for u32 { fn from(hr: HResultError) -> Self { hr.0 } }
impl From<HResultError> for i32 { fn from(hr: HResultError) -> Self { hr.0 as _ } } impl From<(HResultFacilityMicrosoft, ErrorCode)> for HResultError { fn from((fac, code): (HResultFacilityMicrosoft, ErrorCode)) -> Self { Self(0x8000_0000 | (fac.to_u32()<<16) | code.to_u32()) } }
#[allow(useless_deprecated)]
#[deprecated = "allows the construction of 'error' hresults from success values"]
impl From<u32> for HResultError { fn from(hr: u32) -> Self { Self(hr) } }
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Pod, Zeroable)] #[repr(transparent)] pub struct HResult(pub(crate) u32);
impl HResult {
#[doc(hidden)] pub const fn from_constant(value: u32) -> Self { Self(value) }
pub const fn is_error (self) -> bool { self.0 & 0x80000000 != 0 }
pub const fn is_customer(self) -> bool { self.0 & 0x20000000 != 0 }
pub const fn is_ntstatus(self) -> bool { self.0 & 0x10000000 != 0 }
pub const fn facility (self) -> u16 { (self.0 & 0x0FFF0000 >> 16) as _ }
pub const fn code (self) -> u16 { self.0 as _ }
pub const fn to_u32 (self) -> u32 { self.0 }
pub const fn succeeded(self) -> Result<HResultSuccess, HResultError> {
if self.is_error() {
Err(HResultError(self.0))
} else {
Ok(HResultSuccess(self.0))
}
}
}
impl From<HResult> for u32 { fn from(hr: HResult ) -> Self { hr.0 } }
impl From<HResult> for i32 { fn from(hr: HResult ) -> Self { hr.0 as _ } } impl From<u32> for HResult { fn from(hr: u32 ) -> Self { Self(hr) } }
impl From<i32> for HResult { fn from(hr: i32 ) -> Self { Self(hr as _) } } impl From<HResultSuccess> for HResult { fn from(hr: HResultSuccess) -> Self { Self(hr.0) } }
impl From<HResultError> for HResult { fn from(hr: HResultError ) -> Self { Self(hr.0) } }
impl From<(HResultFacilityMicrosoft, ErrorCode)> for HResult { fn from((fac, code): (HResultFacilityMicrosoft, ErrorCode)) -> Self { Self(0x8000_0000 | (fac.to_u32()<<16) | code.to_u32()) } }
macro_rules! compare { ($($a:ident == $z:ident),* $(,)?) => {$(
impl PartialEq<$a> for $z { fn eq(&self, other: &$a) -> bool { HResult::from(*self).0 == HResult::from(*other).0 } }
impl PartialEq<$z> for $a { fn eq(&self, other: &$z) -> bool { HResult::from(*self).0 == HResult::from(*other).0 } }
)*}}
compare! {
HResult == u32,
HResult == i32, HResultSuccess == HResult,
HResultSuccess == u32,
HResultSuccess == i32,
HResultError == HResult,
HResultError == u32,
HResultError == i32,
}
impl<O, E: PartialEq<HResultError>> PartialEq<HResultError> for Result<O, E> {
fn eq(&self, other: &HResultError) -> bool {
match self.as_ref() {
Ok(_) => false,
Err(e) => *e == *other,
}
}
}
impl<O, E: PartialEq<HResultError>> PartialEq<Result<O, E>> for HResultError {
fn eq(&self, other: &Result<O, E>) -> bool {
match other.as_ref() {
Ok(_) => false,
Err(e) => *e == *self,
}
}
}
impl<O: PartialEq<HResultSuccess>, E> PartialEq<HResultSuccess> for Result<O, E> {
fn eq(&self, other: &HResultSuccess) -> bool {
match self.as_ref() {
Ok(o) => *o == *other,
Err(_) => false,
}
}
}
impl<O: PartialEq<HResultSuccess>, E> PartialEq<Result<O, E>> for HResultSuccess {
fn eq(&self, other: &Result<O, E>) -> bool {
match other.as_ref() {
Ok(o) => *o == *self,
Err(_) => false,
}
}
}