Skip to main content

HResult

Struct HResult 

Source
pub struct HResult(/* private fields */);
Expand description

An HRESULT, the signed code COM and much of the Windows API report in.

An i32: FAILED(hr) is hr < 0, so the severity is the sign bit. The layout, from the top: the severity, a reserved bit, the customer bit, the NTSTATUS bit, another reserved bit, the facility, and a 16-bit code. It is a number space of its own, not Win32Error’s: a Win32 code is wrapped into one by from_win32 and read back out by win32, and neither direction is lossless in general (see each).

Success is any non-negative value, S_OK (zero) and S_FALSE (one) among them, which is why into_result can not tell those apart and the raw value is there for a caller who must. It is not an Error, because a success is a value of this type.

§Example

use win_error_codes::{HResult, Win32Error};

// ERROR_IO_DEVICE, wrapped as HRESULT_FROM_WIN32 wraps it.
let hresult = HResult::from_win32(Win32Error::from_code(1117));
assert_eq!(hresult.raw().cast_unsigned(), 0x8007_045D);
assert!(hresult.is_failure());
assert_eq!(hresult.win32(), Some(Win32Error::from_code(1117)));
assert_eq!(hresult.to_string(), "0x8007045D");

// E_FAIL is a failure that wraps no Win32 code.
let e_fail = HResult::from_raw(0x8000_4005_u32.cast_signed());
assert!(e_fail.is_failure());
assert_eq!(e_fail.win32(), None);

Implementations§

Source§

impl HResult

Source

pub const fn from_raw(raw: i32) -> Self

Wraps a raw HRESULT. Any value is accepted.

Source

pub const fn raw(self) -> i32

The raw HRESULT, unaltered.

Source

pub const fn is_failure(self) -> bool

FAILED(hr): whether the severity bit is set, which is whether the value is negative.

Source

pub const fn is_success(self) -> bool

SUCCEEDED(hr): any non-negative value, S_OK and S_FALSE among them.

Source

pub const fn into_result(self) -> Result<(), Self>

Ok for a success and Err carrying this value for a failure.

§Errors

This value, when it is a failure.

Source

pub const fn code(self) -> u16

HRESULT_CODE: the low 16 bits.

Source

pub const fn facility(self) -> u16

HRESULT_FACILITY: the 13 bits above the code, as winerror.h’s macro takes them. That is wider than the 11-bit facility the documentation draws; see the note on the mask.

Source

pub const fn is_customer(self) -> bool

Whether the customer bit is set: a code a third party defined, not Microsoft.

Source

pub const fn is_ntstatus_mapped(self) -> bool

Whether the NTSTATUS bit is set, which is what HRESULT_FROM_NT does to a status.

Source

pub const fn from_win32(error: Win32Error) -> Self

HRESULT_FROM_WIN32: wraps a Win32 code with the severity bit and FACILITY_WIN32.

Not lossless, and that is the macro’s own rule. Zero is S_OK. A code that is not positive as an i32, so one of 2^31 or more, is taken to be an HRESULT already and is returned as it is. Any other code keeps only its low 16 bits: 0x0010045D and 0x0000045D both give 0x8007045D. win32 is the inverse only for the codes from 1 to 65535.

Source

pub const fn win32(self) -> Option<Win32Error>

The Win32 code this wraps, or None if it is not one from_win32 makes: severity set, FACILITY_WIN32, and no other bit above the code.

None for S_OK, though from_win32 of zero is S_OK: a success wraps no error, which is the reading durable-ioring’s ErrorCode already takes and tests. So the round trip holds for the codes from 1 to 65535 and not for zero or anything above.

Source§

impl HResult

Source

pub const fn from_ntstatus(status: NtStatus) -> Self

HRESULT_FROM_NT: the status with the bit that marks an HRESULT made from one set.

Not lossless for a status that already has that bit set: ntstatus clears it, so such a status does not come back as it went in.

Source

pub const fn ntstatus(self) -> Option<NtStatus>

The status this was made from, or None if the bit that HRESULT_FROM_NT sets is clear.

Trait Implementations§

Source§

impl Clone for HResult

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for HResult

Source§

impl Debug for HResult

HResult(0x8007045D), in hex and not as the negative number the i32 is.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for HResult

The value in hex, as winerror.h writes it: 0x8007045D.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for HResult

Source§

impl Hash for HResult

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for HResult

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for HResult

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.