win-error-codes
The numbers Windows reports failure in, as types.
Windows reports a failure as a number, and which number space it is in is not in the number. A
DWORD from GetLastError is one; a HRESULT is another. Crates here each kept a bare u32 or
wrote a Win32Error of their own, and the two that did disagreed about a code of 2^31 or more,
which exist: the virtdisk errors, such as ERROR_VIRTDISK_NOT_VIRTUAL_DISK, are WIN32_ERRORs of
3225026581. This crate gives each number space its own type, and the conversion to and from
std::io::Error is written once and is exact for every code.
What it gives
Win32Error, au32: the code unaltered,is_successandinto_result(zero isERROR_SUCCESS), and on Windowsto_io_error,from_ioandlast.LStatus, ani32: the same number space in the signedLONGthat registry calls return, converting to and fromWin32Errorby a bit cast that loses nothing, a code above 2^31 being a negative value.HResult, ani32: the signedHRESULT, withis_failureandis_success,code,facility,from_win32(HRESULT_FROM_WIN32's rule, which keeps only the low 16 bits of a positive code) andwin32(the inverse for the codes from 1 to 65535).NtStatus, ani32: the signedNTSTATUS, with itsNtSeverity,NT_SUCCESSand the other three predicates,code,facility, andHResult::from_ntstatus(HRESULT_FROM_NT). There is no conversion to a Win32 code: that is anRtl*call.
use ;
// A call that returns its code directly: zero is success.
assert_eq!;
// A registry call returns the same thing as a signed LONG.
assert_eq!;
// ERROR_IO_DEVICE, wrapped as an HRESULT.
let hresult = from_win32;
assert_eq!;
assert_eq!;
// STATUS_ACCESS_VIOLATION: an error, and not a success. HRESULT_FROM_NT sets one bit.
let status = from_raw;
assert!;
assert_eq!;
// ERROR_VIRTDISK_NOT_VIRTUAL_DISK, a code above 2^31, keeps its value through io::Error.
#
What it does not do
- No named constants.
windows-syshas them. - No mapping onto a friendlier taxonomy. A type carries what Windows said.
- No
unsafeand no native API surface. The numbers and their conversions build on every target. The bridge toio::Erroris Windows-only, because off Windows a raw OS error is anerrno, and reading one as a Win32 code would be a wrong answer.
The decisions are in DESIGN-NOTES.md and the plan is in CHECKLIST.md.