Expand description
Lightweight NTSTATUS-based error handling for Windows kernel-mode Rust code.
ntresult provides a minimal and idiomatic interface for working with Windows
NTSTATUS values in Rust, especially in #![no_std] and kernel-mode
environments.
§Overview
- Converts an
NTSTATUSinto aResult, and collapses one back out withNtStatusorNtStatusOrSuccess - Represents failures as
Error, a transparentNTSTATUSwrapper - Carries an expected non-success status as
Statusin aStatusResult - Inspects a status through
Severity, facility and code accessors - Converts selected
coreandallocerrors viacommon_error - Offers shorthand macros –
ntres!,ntok!,nterr!, the_retvariants that return immediately, andntbail!
§Design
ntresult treats only STATUS_SUCCESS as success. All other NTSTATUS values,
including warning and informational codes, are treated as errors unless they
are intentionally returned as a Status inside a StatusResult.
The crate contains no unsafe code, enforced with #![forbid(unsafe_code)].
§Example
use ntresult::{IntoResult, NtStatus, Result};
use windows_sys::Win32::Foundation::{NTSTATUS, STATUS_SUCCESS};
// Stand-in for a kernel API returning a raw status.
fn open_device() -> NTSTATUS {
STATUS_SUCCESS
}
fn init_driver() -> Result<()> {
open_device().into_result()?; // NTSTATUS -> Result, failures propagate
Ok(())
}
fn driver_entry() -> NTSTATUS {
init_driver().ntstatus() // Result -> NTSTATUS for the kernel
}
assert_eq!(driver_entry(), STATUS_SUCCESS);§Features
All features are additive: each one only adds From conversions into Error.
Enabling a feature never changes the behaviour of an existing conversion.
| Feature | Default | Toolchain | Conversion added |
|---|---|---|---|
alloc | no | stable | alloc::collections::TryReserveError |
allocator-api | no | nightly | core::alloc::AllocError |
Conversions that need nothing beyond core – core::num::TryFromIntError
and core::net::AddrParseError – are always available and are not gated.
There are no default features. Enabling alloc links the alloc crate,
which makes rustc require a #[global_allocator] from the final artifact
even if nothing ever allocates – so a driver that only needs the NTSTATUS
wrapper is not made to supply one.
allocator-api enables the unstable allocator_api language feature and so
requires a nightly compiler. Everything else builds on stable Rust.
allocator-api does not imply alloc: AllocError lives in core, so the
conversion is available even without an allocator.
More features may be added in the future to support additional common error types.
Modules§
- common_
error - Conversions from common Rust error types into
Error.
Macros§
- ntbail
- Wrap an
NTSTATUSin anErrorand return it asErrimmediately. - nterr
- Wrap an
NTSTATUSin anErrorand produce it asErr. - nterr_
ret - Wrap an
NTSTATUSin anErrorand return it asErrimmediately. - ntok
- Shorthand for
Ok(value). - ntok_
ret - Shorthand for
return Ok(value). - ntres
- Convert an
NTSTATUSinto aResult<(), Error>. - ntres_
ret - Convert an
NTSTATUSinto aResult<(), Error>and return it immediately.
Structs§
- Error
- The error type representing [
NTSTATUS] codes. - Status
- A status that is an expected outcome rather than a failure.
Enums§
- Severity
- The severity class encoded in the top two bits of an [
NTSTATUS].
Traits§
- Into
Error - Convert a value into an
Error. - Into
Result - Convert a value into a
Resultwhose error type isError. - NtStatus
- Retrieve the [
NTSTATUS] code that a value represents. - NtStatus
OrSuccess - Collapse any
Resultinto anNTSTATUS, treating everyOkas success.
Type Aliases§
- Result
- A specialized
Resulttype used throughout kernel-mode driver code, where errors are represented by Windows [NTSTATUS] codes. - Status
Result - A
Resultwhose success case carries aStatusto be returned verbatim.