Skip to content

Commit a6623f2

Browse files
authored
feat: add more precise NTSTATUS const fns (#183)
1 parent c471d8f commit a6623f2

File tree

7 files changed

+1031
-1
lines changed

7 files changed

+1031
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exclude = [
1010
"tests/config-umdf",
1111
"tests/config-wdm",
1212
"tests/wdk-macros-tests",
13+
"tests/wdk-sys-tests",
1314
]
1415
resolver = "2"
1516

crates/wdk-sys/src/lib.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,72 @@ pub extern "system" fn __CxxFrameHandler3() -> i32 {
7474
driver_model__driver_type = "KMDF",
7575
driver_model__driver_type = "UMDF"
7676
))]
77-
#[allow(missing_docs)]
7877
#[must_use]
7978
#[allow(non_snake_case)]
79+
/// Evaluates to TRUE if the return value specified by `nt_status` is a success
80+
/// type (0 − 0x3FFFFFFF) or an informational type (0x40000000 − 0x7FFFFFFF).
81+
/// This function is taken from ntdef.h in the WDK.
82+
///
83+
/// See the [NTSTATUS reference](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781) and
84+
/// [Using NTSTATUS values](https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/using-ntstatus-values) for details.
8085
pub const fn NT_SUCCESS(nt_status: NTSTATUS) -> bool {
8186
nt_status >= 0
8287
}
8388

89+
#[cfg(any(
90+
driver_model__driver_type = "WDM",
91+
driver_model__driver_type = "KMDF",
92+
driver_model__driver_type = "UMDF"
93+
))]
94+
#[must_use]
95+
#[allow(non_snake_case)]
96+
#[allow(clippy::cast_sign_loss)]
97+
/// Evaluates to TRUE if the return value specified by `nt_status` is an
98+
/// informational type (0x40000000 − 0x7FFFFFFF). This function is taken from
99+
/// ntdef.h in the WDK.
100+
///
101+
/// See the [NTSTATUS reference](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781) and
102+
/// [Using NTSTATUS values](https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/using-ntstatus-values) for details.
103+
pub const fn NT_INFORMATION(nt_status: NTSTATUS) -> bool {
104+
(nt_status as u32 >> 30) == 1
105+
}
106+
107+
#[cfg(any(
108+
driver_model__driver_type = "WDM",
109+
driver_model__driver_type = "KMDF",
110+
driver_model__driver_type = "UMDF"
111+
))]
112+
#[must_use]
113+
#[allow(non_snake_case)]
114+
#[allow(clippy::cast_sign_loss)]
115+
/// Evaluates to TRUE if the return value specified by `nt_status` is a warning
116+
/// type (0x80000000 − 0xBFFFFFFF). This function is taken from ntdef.h in the
117+
/// WDK.
118+
///
119+
/// See the [NTSTATUS reference](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781) and
120+
/// [Using NTSTATUS values](https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/using-ntstatus-values) for details.
121+
pub const fn NT_WARNING(nt_status: NTSTATUS) -> bool {
122+
(nt_status as u32 >> 30) == 2
123+
}
124+
125+
#[cfg(any(
126+
driver_model__driver_type = "WDM",
127+
driver_model__driver_type = "KMDF",
128+
driver_model__driver_type = "UMDF"
129+
))]
130+
#[must_use]
131+
#[allow(non_snake_case)]
132+
#[allow(clippy::cast_sign_loss)]
133+
/// Evaluates to TRUE if the return value specified by `nt_status` is an error
134+
/// type (0xC0000000 - 0xFFFFFFFF). This function is taken from ntdef.h in the
135+
/// WDK.
136+
///
137+
/// See the [NTSTATUS reference](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781) and
138+
/// [Using NTSTATUS values](https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/using-ntstatus-values) for details.
139+
pub const fn NT_ERROR(nt_status: NTSTATUS) -> bool {
140+
(nt_status as u32 >> 30) == 3
141+
}
142+
84143
#[cfg(any(driver_model__driver_type = "WDM", driver_model__driver_type = "KMDF"))]
85144
#[allow(missing_docs)]
86145
#[macro_export]

tests/Makefile.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ CARGO_MAKE_CRATE_WORKSPACE_MEMBERS = [
99
"mixed-package-kmdf-workspace",
1010
"umdf-driver-workspace",
1111
"wdk-macros-tests",
12+
"wdk-sys-tests",
1213
]

0 commit comments

Comments
 (0)