winresult_types/
ntstatus.rs

1use bytemuck::*;
2use core::convert::Infallible;
3
4
5
6/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781)\]
7/// NTSTATUS
8#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Pod, Zeroable)] #[repr(transparent)] pub struct NtStatus(pub(crate) u32);
9
10impl NtStatus {
11    #[doc(hidden)] pub const fn from_constant(value: u32) -> Self { Self(value) }
12
13    pub const fn sev                (self) -> NtStatusSeverity { NtStatusSeverity((self.0 >> 30) as _) }
14
15    pub const fn is_error           (self) -> bool { self.sev().0 == 3 }
16    pub const fn is_warning         (self) -> bool { self.sev().0 == 2 }
17    pub const fn is_informational   (self) -> bool { self.sev().0 == 1 }
18    pub const fn is_success         (self) -> bool { self.sev().0 == 0 }
19
20    pub const fn is_customer        (self) -> bool { self.0 & 0x20000000 != 0 }
21    pub const fn is_ntstatus        (self) -> bool { self.0 & 0x10000000 != 0 }
22
23    pub const fn facility           (self) -> u16  { (self.0 & 0x0FFF0000 >> 16) as _ }
24    pub const fn code               (self) -> u16  { self.0 as _ }
25    pub const fn to_u32             (self) -> u32  { self.0 }
26}
27
28impl From<NtStatus> for u32 { fn from(hr: NtStatus) -> Self { hr.0 } }
29impl From<u32> for NtStatus { fn from(hr: u32) -> Self { Self(hr) } }
30impl From<Infallible> for NtStatus { fn from(i: Infallible) -> Self { match i {} } }
31
32
33
34/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781)\]
35/// NtStatus::Sev
36#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct NtStatusSeverity(pub(crate) u8);
37
38impl NtStatusSeverity {
39    #[doc(hidden)] pub const fn from_constant(value: u8) -> Self { assert!(value < 4, "NTSTATUS severities are only 2 bits"); Self(value) }
40
41    pub const fn to_u8 (self) ->  u8 { self.0 }
42    pub const fn to_u16(self) -> u16 { self.0 as _ }
43    pub const fn to_u32(self) -> u32 { self.0 as _ }
44}
45
46
47
48/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781)\]
49/// FACILITY_\* values corresponding to Microsoft (non-customer) `NTSTATUS`es.
50///
51/// Note that HRESULT facilities, despite also being prefixed with FACILITY_\*, are incompatible (overlapping values interpreted differently!)
52#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct NtStatusFacilityMicrosoft(pub(crate) u16);
53
54impl NtStatusFacilityMicrosoft {
55    #[doc(hidden)] pub const fn from_constant(value: u16) -> Self { assert!(value <= 0xFFF, "NTSTATUS facilities are only 12 bits"); Self(value) }
56
57    pub const fn to_u16(self) -> u16 { self.0 }
58    pub const fn to_u32(self) -> u32 { self.0 as _ }
59}
60
61impl From<NtStatusFacilityMicrosoft> for u16 { fn from(f: NtStatusFacilityMicrosoft) -> Self { f.0 } }
62impl From<NtStatusFacilityMicrosoft> for u32 { fn from(f: NtStatusFacilityMicrosoft) -> Self { f.0.into() } }