Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

83
vendor/windows-result/src/bindings.rs vendored Normal file
View File

@@ -0,0 +1,83 @@
windows_link::link!("kernel32.dll" "system" fn FormatMessageW(dwflags : FORMAT_MESSAGE_OPTIONS, lpsource : *const core::ffi::c_void, dwmessageid : u32, dwlanguageid : u32, lpbuffer : PWSTR, nsize : u32, arguments : *const *const i8) -> u32);
windows_link::link!("oleaut32.dll" "system" fn GetErrorInfo(dwreserved : u32, pperrinfo : *mut * mut core::ffi::c_void) -> HRESULT);
windows_link::link!("kernel32.dll" "system" fn GetLastError() -> WIN32_ERROR);
windows_link::link!("kernel32.dll" "system" fn GetProcessHeap() -> HANDLE);
windows_link::link!("kernel32.dll" "system" fn HeapFree(hheap : HANDLE, dwflags : HEAP_FLAGS, lpmem : *const core::ffi::c_void) -> BOOL);
windows_link::link!("kernel32.dll" "system" fn LoadLibraryExA(lplibfilename : PCSTR, hfile : HANDLE, dwflags : LOAD_LIBRARY_FLAGS) -> HMODULE);
windows_link::link!("api-ms-win-core-winrt-error-l1-1-0.dll" "system" fn RoOriginateErrorW(error : HRESULT, cchmax : u32, message : PCWSTR) -> BOOL);
windows_link::link!("oleaut32.dll" "system" fn SetErrorInfo(dwreserved : u32, perrinfo : * mut core::ffi::c_void) -> HRESULT);
windows_link::link!("oleaut32.dll" "system" fn SysFreeString(bstrstring : BSTR));
windows_link::link!("oleaut32.dll" "system" fn SysStringLen(pbstr : BSTR) -> u32);
pub type BOOL = i32;
pub type BSTR = *const u16;
pub const ERROR_INVALID_DATA: WIN32_ERROR = 13u32;
pub const ERROR_NO_UNICODE_TRANSLATION: WIN32_ERROR = 1113u32;
pub const E_UNEXPECTED: HRESULT = 0x8000FFFF_u32 as _;
pub const FORMAT_MESSAGE_ALLOCATE_BUFFER: FORMAT_MESSAGE_OPTIONS = 256u32;
pub const FORMAT_MESSAGE_FROM_HMODULE: FORMAT_MESSAGE_OPTIONS = 2048u32;
pub const FORMAT_MESSAGE_FROM_SYSTEM: FORMAT_MESSAGE_OPTIONS = 4096u32;
pub const FORMAT_MESSAGE_IGNORE_INSERTS: FORMAT_MESSAGE_OPTIONS = 512u32;
pub type FORMAT_MESSAGE_OPTIONS = u32;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct GUID {
pub data1: u32,
pub data2: u16,
pub data3: u16,
pub data4: [u8; 8],
}
impl GUID {
pub const fn from_u128(uuid: u128) -> Self {
Self {
data1: (uuid >> 96) as u32,
data2: (uuid >> 80 & 0xffff) as u16,
data3: (uuid >> 64 & 0xffff) as u16,
data4: (uuid as u64).to_be_bytes(),
}
}
}
pub type HANDLE = *mut core::ffi::c_void;
pub type HEAP_FLAGS = u32;
pub type HINSTANCE = *mut core::ffi::c_void;
pub type HMODULE = *mut core::ffi::c_void;
pub type HRESULT = i32;
pub const IID_IErrorInfo: GUID = GUID::from_u128(0x1cf2b120_547d_101b_8e65_08002b2bd119);
#[repr(C)]
pub struct IErrorInfo_Vtbl {
pub base__: IUnknown_Vtbl,
pub GetGUID: unsafe extern "system" fn(*mut core::ffi::c_void, *mut GUID) -> HRESULT,
pub GetSource: unsafe extern "system" fn(*mut core::ffi::c_void, *mut BSTR) -> HRESULT,
pub GetDescription: unsafe extern "system" fn(*mut core::ffi::c_void, *mut BSTR) -> HRESULT,
pub GetHelpFile: unsafe extern "system" fn(*mut core::ffi::c_void, *mut BSTR) -> HRESULT,
pub GetHelpContext: unsafe extern "system" fn(*mut core::ffi::c_void, *mut u32) -> HRESULT,
}
pub const IID_IRestrictedErrorInfo: GUID = GUID::from_u128(0x82ba7092_4c88_427d_a7bc_16dd93feb67e);
#[repr(C)]
pub struct IRestrictedErrorInfo_Vtbl {
pub base__: IUnknown_Vtbl,
pub GetErrorDetails: unsafe extern "system" fn(
*mut core::ffi::c_void,
*mut BSTR,
*mut HRESULT,
*mut BSTR,
*mut BSTR,
) -> HRESULT,
pub GetReference: unsafe extern "system" fn(*mut core::ffi::c_void, *mut BSTR) -> HRESULT,
}
pub const IID_IUnknown: GUID = GUID::from_u128(0x00000000_0000_0000_c000_000000000046);
#[repr(C)]
pub struct IUnknown_Vtbl {
pub QueryInterface: unsafe extern "system" fn(
this: *mut core::ffi::c_void,
iid: *const GUID,
interface: *mut *mut core::ffi::c_void,
) -> HRESULT,
pub AddRef: unsafe extern "system" fn(this: *mut core::ffi::c_void) -> u32,
pub Release: unsafe extern "system" fn(this: *mut core::ffi::c_void) -> u32,
}
pub type LOAD_LIBRARY_FLAGS = u32;
pub const LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: LOAD_LIBRARY_FLAGS = 4096u32;
pub type PCSTR = *const u8;
pub type PCWSTR = *const u16;
pub type PWSTR = *mut u16;
pub type WIN32_ERROR = u32;

90
vendor/windows-result/src/bool.rs vendored Normal file
View File

@@ -0,0 +1,90 @@
use super::*;
/// A 32-bit value representing boolean values and returned by some functions to indicate success or failure.
#[must_use]
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct BOOL(pub i32);
impl BOOL {
/// Converts the [`BOOL`] to a [`prim@bool`] value.
#[inline]
pub fn as_bool(self) -> bool {
self.0 != 0
}
/// Converts the [`BOOL`] to [`Result<()>`][Result<_>].
#[inline]
pub fn ok(self) -> Result<()> {
if self.as_bool() {
Ok(())
} else {
Err(Error::from_thread())
}
}
/// Asserts that `self` is a success code.
#[inline]
#[track_caller]
pub fn unwrap(self) {
self.ok().unwrap();
}
/// Asserts that `self` is a success code using the given panic message.
#[inline]
#[track_caller]
pub fn expect(self, msg: &str) {
self.ok().expect(msg);
}
}
impl From<BOOL> for bool {
fn from(value: BOOL) -> Self {
value.as_bool()
}
}
impl From<&BOOL> for bool {
fn from(value: &BOOL) -> Self {
value.as_bool()
}
}
impl From<bool> for BOOL {
fn from(value: bool) -> Self {
if value {
Self(1)
} else {
Self(0)
}
}
}
impl From<&bool> for BOOL {
fn from(value: &bool) -> Self {
(*value).into()
}
}
impl PartialEq<bool> for BOOL {
fn eq(&self, other: &bool) -> bool {
self.as_bool() == *other
}
}
impl PartialEq<BOOL> for bool {
fn eq(&self, other: &BOOL) -> bool {
*self == other.as_bool()
}
}
impl core::ops::Not for BOOL {
type Output = Self;
fn not(self) -> Self::Output {
if self.as_bool() {
Self(0)
} else {
Self(1)
}
}
}

40
vendor/windows-result/src/bstr.rs vendored Normal file
View File

@@ -0,0 +1,40 @@
use super::*;
use core::ops::Deref;
#[repr(transparent)]
pub struct BasicString(*const u16);
impl Deref for BasicString {
type Target = [u16];
fn deref(&self) -> &[u16] {
let len = if self.0.is_null() {
0
} else {
unsafe { SysStringLen(self.0) as usize }
};
if len > 0 {
unsafe { core::slice::from_raw_parts(self.0, len) }
} else {
// This ensures that if `as_ptr` is called on the slice that the resulting pointer
// will still refer to a null-terminated string.
const EMPTY: [u16; 1] = [0];
&EMPTY[..0]
}
}
}
impl Default for BasicString {
fn default() -> Self {
Self(core::ptr::null_mut())
}
}
impl Drop for BasicString {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe { SysFreeString(self.0) }
}
}
}

54
vendor/windows-result/src/com.rs vendored Normal file
View File

@@ -0,0 +1,54 @@
use super::*;
#[doc(hidden)]
#[macro_export]
macro_rules! com_call {
($vtbl:ty, $this:ident.$method:ident($($args:tt)*)) => {
((&**($this.as_raw() as *mut *mut $vtbl)).$method)($this.as_raw(), $($args)*)
}
}
#[repr(transparent)]
pub struct ComPtr(core::ptr::NonNull<core::ffi::c_void>);
impl ComPtr {
pub fn as_raw(&self) -> *mut core::ffi::c_void {
unsafe { core::mem::transmute_copy(self) }
}
pub fn cast(&self, iid: &GUID) -> Option<Self> {
let mut result = None;
unsafe {
com_call!(
IUnknown_Vtbl,
self.QueryInterface(iid, &mut result as *mut _ as _)
);
}
result
}
}
impl PartialEq for ComPtr {
fn eq(&self, other: &Self) -> bool {
self.cast(&IID_IUnknown).unwrap().0 == other.cast(&IID_IUnknown).unwrap().0
}
}
impl Eq for ComPtr {}
impl Clone for ComPtr {
fn clone(&self) -> Self {
unsafe {
com_call!(IUnknown_Vtbl, self.AddRef());
}
Self(self.0)
}
}
impl Drop for ComPtr {
fn drop(&mut self) {
unsafe {
com_call!(IUnknown_Vtbl, self.Release());
}
}
}

388
vendor/windows-result/src/error.rs vendored Normal file
View File

@@ -0,0 +1,388 @@
use super::*;
use core::num::NonZeroI32;
#[expect(unused_imports)]
use core::mem::size_of;
/// An error object consists of both an error code and optional detailed error information for debugging.
///
/// # Extended error info and the `windows_slim_errors` configuration option
///
/// `Error` contains an [`HRESULT`] value that describes the error, as well as an optional
/// `IErrorInfo` COM object. The `IErrorInfo` object is a COM object that can provide detailed information
/// about an error, such as a text string, a `ProgID` of the originator, etc. If the error object
/// was originated in an WinRT component, then additional information such as a stack track may be
/// captured.
///
/// However, many systems based on COM do not use `IErrorInfo`. For these systems, the optional error
/// info within `Error` has no benefits, but has substantial costs because it increases the size of
/// the `Error` object, which also increases the size of `Result<T>`.
///
/// This error information can be disabled at compile time by setting `RUSTFLAGS=--cfg=windows_slim_errors`.
/// This removes the `IErrorInfo` support within the [`Error`] type, which has these benefits:
///
/// * It reduces the size of [`Error`] to 4 bytes (the size of [`HRESULT`]).
///
/// * It reduces the size of `Result<(), Error>` to 4 bytes, allowing it to be returned in a single
/// machine register.
///
/// * The `Error` (and `Result<T, Error>`) types no longer have a [`Drop`] impl. This removes the need
/// for lifetime checking and running drop code when [`Error`] and [`Result`] go out of scope. This
/// significantly reduces code size for codebase that make extensive use of [`Error`].
///
/// Of course, these benefits come with a cost; you lose extended error information for those
/// COM objects that support it.
///
/// This is controlled by a `--cfg` option rather than a Cargo feature because this compilation
/// option sets a policy that applies to an entire graph of crates. Individual crates that take a
/// dependency on the `windows-result` crate are not in a good position to decide whether they want
/// slim errors or full errors. Cargo features are meant to be additive, but specifying the size
/// and contents of `Error` is not a feature so much as a whole-program policy decision.
///
/// # References
///
/// * [`IErrorInfo`](https://learn.microsoft.com/en-us/windows/win32/api/oaidl/nn-oaidl-ierrorinfo)
#[derive(Clone)]
pub struct Error {
/// The `HRESULT` error code, but represented using [`NonZeroI32`]. [`NonZeroI32`] provides
/// a "niche" to the Rust compiler, which is a space-saving optimization. This allows the
/// compiler to use more compact representation for enum variants (such as [`Result`]) that
/// contain instances of [`Error`].
code: NonZeroI32,
/// Contains details about the error, such as error text.
info: ErrorInfo,
}
/// We remap S_OK to this error because the S_OK representation (zero) is reserved for niche
/// optimizations.
const S_EMPTY_ERROR: NonZeroI32 = const_nonzero_i32(u32::from_be_bytes(*b"S_OK") as i32);
/// Converts an HRESULT into a NonZeroI32. If the input is S_OK (zero), then this is converted to
/// S_EMPTY_ERROR. This is necessary because NonZeroI32, as the name implies, cannot represent the
/// value zero. So we remap it to a value no one should be using, during storage.
const fn const_nonzero_i32(i: i32) -> NonZeroI32 {
if let Some(nz) = NonZeroI32::new(i) {
nz
} else {
panic!();
}
}
fn nonzero_hresult(hr: HRESULT) -> NonZeroI32 {
if let Some(nz) = NonZeroI32::new(hr.0) {
nz
} else {
S_EMPTY_ERROR
}
}
impl Error {
/// Creates an error object without any failure information.
pub const fn empty() -> Self {
Self {
code: S_EMPTY_ERROR,
info: ErrorInfo::empty(),
}
}
/// Creates a new error object, capturing the stack and other information about the
/// point of failure.
pub fn new<T: AsRef<str>>(code: HRESULT, message: T) -> Self {
#[cfg(windows)]
{
let message: &str = message.as_ref();
if message.is_empty() {
Self::from_hresult(code)
} else {
ErrorInfo::originate_error(code, message);
code.into()
}
}
#[cfg(not(windows))]
{
let _ = message;
Self::from_hresult(code)
}
}
/// Creates a new error object with an error code, but without additional error information.
pub fn from_hresult(code: HRESULT) -> Self {
Self {
code: nonzero_hresult(code),
info: ErrorInfo::empty(),
}
}
/// Creates a new `Error` from the Win32 error code returned by `GetLastError()`.
pub fn from_thread() -> Self {
Self::from_hresult(HRESULT::from_thread())
}
/// The error code describing the error.
pub const fn code(&self) -> HRESULT {
if self.code.get() == S_EMPTY_ERROR.get() {
HRESULT(0)
} else {
HRESULT(self.code.get())
}
}
/// The error message describing the error.
pub fn message(&self) -> String {
if let Some(message) = self.info.message() {
return message;
}
// Otherwise fallback to a generic error code description.
self.code().message()
}
/// The error object describing the error.
#[cfg(windows)]
pub fn as_ptr(&self) -> *mut core::ffi::c_void {
self.info.as_ptr()
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl From<Error> for HRESULT {
fn from(error: Error) -> Self {
let code = error.code();
error.info.into_thread();
code
}
}
impl From<HRESULT> for Error {
fn from(code: HRESULT) -> Self {
Self {
code: nonzero_hresult(code),
info: ErrorInfo::from_thread(),
}
}
}
#[cfg(feature = "std")]
impl From<Error> for std::io::Error {
fn from(from: Error) -> Self {
Self::from_raw_os_error(from.code().0)
}
}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(from: std::io::Error) -> Self {
match from.raw_os_error() {
Some(status) => HRESULT::from_win32(status as u32).into(),
None => HRESULT(E_UNEXPECTED).into(),
}
}
}
impl From<alloc::string::FromUtf16Error> for Error {
fn from(_: alloc::string::FromUtf16Error) -> Self {
Self::from_hresult(HRESULT::from_win32(ERROR_NO_UNICODE_TRANSLATION))
}
}
impl From<alloc::string::FromUtf8Error> for Error {
fn from(_: alloc::string::FromUtf8Error) -> Self {
Self::from_hresult(HRESULT::from_win32(ERROR_NO_UNICODE_TRANSLATION))
}
}
impl From<core::num::TryFromIntError> for Error {
fn from(_: core::num::TryFromIntError) -> Self {
Self::from_hresult(HRESULT::from_win32(ERROR_INVALID_DATA))
}
}
impl core::fmt::Debug for Error {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
let mut debug = fmt.debug_struct("Error");
debug
.field("code", &self.code())
.field("message", &self.message())
.finish()
}
}
impl core::fmt::Display for Error {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
let message = self.message();
if message.is_empty() {
core::write!(fmt, "{}", self.code())
} else {
core::write!(fmt, "{} ({})", message, self.code())
}
}
}
impl core::hash::Hash for Error {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.code.hash(state);
// We do not hash the error info.
}
}
// Equality tests only the HRESULT, not the error info (if any).
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
self.code == other.code
}
}
impl Eq for Error {}
impl PartialOrd for Error {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Error {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.code.cmp(&other.code)
}
}
use error_info::*;
#[cfg(all(windows, not(windows_slim_errors)))]
mod error_info {
use super::*;
use crate::com::ComPtr;
/// This type stores error detail, represented by a COM `IErrorInfo` object.
///
/// # References
///
/// * [`IErrorInfo`](https://learn.microsoft.com/en-us/windows/win32/api/oaidl/nn-oaidl-ierrorinfo)
#[derive(Clone, Default)]
pub(crate) struct ErrorInfo {
pub(super) ptr: Option<ComPtr>,
}
impl ErrorInfo {
pub(crate) const fn empty() -> Self {
Self { ptr: None }
}
pub(crate) fn from_thread() -> Self {
unsafe {
let mut ptr = core::mem::MaybeUninit::zeroed();
crate::bindings::GetErrorInfo(0, ptr.as_mut_ptr() as *mut _);
Self {
ptr: ptr.assume_init(),
}
}
}
pub(crate) fn into_thread(self) {
if let Some(ptr) = self.ptr {
unsafe {
crate::bindings::SetErrorInfo(0, ptr.as_raw());
}
}
}
pub(crate) fn originate_error(code: HRESULT, message: &str) {
let message: Vec<_> = message.encode_utf16().collect();
unsafe {
RoOriginateErrorW(code.0, message.len() as u32, message.as_ptr());
}
}
pub(crate) fn message(&self) -> Option<String> {
use crate::bstr::BasicString;
let ptr = self.ptr.as_ref()?;
let mut message = BasicString::default();
// First attempt to retrieve the restricted error information.
if let Some(info) = ptr.cast(&IID_IRestrictedErrorInfo) {
let mut fallback = BasicString::default();
let mut code = 0;
unsafe {
com_call!(
IRestrictedErrorInfo_Vtbl,
info.GetErrorDetails(
&mut fallback as *mut _ as _,
&mut code,
&mut message as *mut _ as _,
&mut BasicString::default() as *mut _ as _
)
);
}
if message.is_empty() {
message = fallback
};
}
// Next attempt to retrieve the regular error information.
if message.is_empty() {
unsafe {
com_call!(
IErrorInfo_Vtbl,
ptr.GetDescription(&mut message as *mut _ as _)
);
}
}
Some(String::from_utf16_lossy(wide_trim_end(&message)))
}
pub(crate) fn as_ptr(&self) -> *mut core::ffi::c_void {
if let Some(info) = self.ptr.as_ref() {
info.as_raw()
} else {
core::ptr::null_mut()
}
}
}
unsafe impl Send for ErrorInfo {}
unsafe impl Sync for ErrorInfo {}
}
#[cfg(not(all(windows, not(windows_slim_errors))))]
mod error_info {
use super::*;
// We use this name so that the NatVis <Type> element for ErrorInfo does *not* match this type.
// This prevents the NatVis description from failing to load.
#[derive(Clone, Default)]
pub(crate) struct EmptyErrorInfo;
pub(crate) use EmptyErrorInfo as ErrorInfo;
impl EmptyErrorInfo {
pub(crate) const fn empty() -> Self {
Self
}
pub(crate) fn from_thread() -> Self {
Self
}
pub(crate) fn into_thread(self) {}
#[cfg(windows)]
pub(crate) fn originate_error(_code: HRESULT, _message: &str) {}
pub(crate) fn message(&self) -> Option<String> {
None
}
#[cfg(windows)]
pub(crate) fn as_ptr(&self) -> *mut core::ffi::c_void {
core::ptr::null_mut()
}
}
}

166
vendor/windows-result/src/hresult.rs vendored Normal file
View File

@@ -0,0 +1,166 @@
use super::*;
/// An error code value returned by most COM functions.
#[repr(transparent)]
#[derive(Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[must_use]
pub struct HRESULT(pub i32);
impl HRESULT {
/// Returns [`true`] if `self` is a success code.
#[inline]
pub const fn is_ok(self) -> bool {
self.0 >= 0
}
/// Returns [`true`] if `self` is a failure code.
#[inline]
pub const fn is_err(self) -> bool {
!self.is_ok()
}
/// Asserts that `self` is a success code.
///
/// This will invoke the [`panic!`] macro if `self` is a failure code and display
/// the [`HRESULT`] value for diagnostics.
#[inline]
#[track_caller]
pub fn unwrap(self) {
assert!(self.is_ok(), "HRESULT 0x{:X}", self.0);
}
/// Converts the [`HRESULT`] to [`Result<()>`][Result<_>].
#[inline]
pub fn ok(self) -> Result<()> {
if self.is_ok() {
Ok(())
} else {
Err(self.into())
}
}
/// Calls `op` if `self` is a success code, otherwise returns [`HRESULT`]
/// converted to [`Result<T>`].
#[inline]
pub fn map<F, T>(self, op: F) -> Result<T>
where
F: FnOnce() -> T,
{
self.ok()?;
Ok(op())
}
/// Calls `op` if `self` is a success code, otherwise returns [`HRESULT`]
/// converted to [`Result<T>`].
#[inline]
pub fn and_then<F, T>(self, op: F) -> Result<T>
where
F: FnOnce() -> Result<T>,
{
self.ok()?;
op()
}
/// The error message describing the error.
pub fn message(self) -> String {
#[cfg(windows)]
{
let mut message = HeapString::default();
let mut code = self.0;
let mut module = core::ptr::null_mut();
let mut flags = FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS;
unsafe {
if self.0 & 0x1000_0000 == 0x1000_0000 {
code ^= 0x1000_0000;
flags |= FORMAT_MESSAGE_FROM_HMODULE;
module = LoadLibraryExA(
c"ntdll.dll".as_ptr() as _,
core::ptr::null_mut(),
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS,
);
}
let size = FormatMessageW(
flags,
module as _,
code as _,
0,
&mut message.0 as *mut _ as *mut _,
0,
core::ptr::null(),
);
if !message.0.is_null() && size > 0 {
String::from_utf16_lossy(wide_trim_end(core::slice::from_raw_parts(
message.0,
size as usize,
)))
} else {
String::default()
}
}
}
#[cfg(not(windows))]
{
return alloc::format!("0x{:08x}", self.0 as u32);
}
}
/// Creates a new `HRESULT` from the Win32 error code returned by `GetLastError()`.
pub fn from_thread() -> Self {
#[cfg(windows)]
{
let error = unsafe { GetLastError() };
Self::from_win32(error)
}
#[cfg(not(windows))]
{
unimplemented!()
}
}
/// Maps a Win32 error code to an HRESULT value.
pub const fn from_win32(error: u32) -> Self {
Self(if error as i32 <= 0 {
error
} else {
(error & 0x0000_FFFF) | (7 << 16) | 0x8000_0000
} as i32)
}
/// Maps an NT error code to an HRESULT value.
pub const fn from_nt(error: i32) -> Self {
Self(if error >= 0 {
error
} else {
error | 0x1000_0000
})
}
}
impl<T> From<Result<T>> for HRESULT {
fn from(result: Result<T>) -> Self {
if let Err(error) = result {
return error.into();
}
Self(0)
}
}
impl core::fmt::Display for HRESULT {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_fmt(format_args!("{:#010X}", self.0))
}
}
impl core::fmt::Debug for HRESULT {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_fmt(format_args!("HRESULT({self})"))
}
}

41
vendor/windows-result/src/lib.rs vendored Normal file
View File

@@ -0,0 +1,41 @@
#![doc = include_str!("../readme.md")]
#![debugger_visualizer(natvis_file = "../windows-result.natvis")]
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#![cfg_attr(not(windows), expect(unused_imports))]
#![expect(
dead_code,
non_upper_case_globals,
non_snake_case,
non_camel_case_types,
clippy::upper_case_acronyms
)]
extern crate alloc;
use alloc::{string::String, vec::Vec};
mod bindings;
use bindings::*;
#[cfg(all(windows, not(windows_slim_errors)))]
mod com;
#[cfg(windows)]
mod strings;
#[cfg(windows)]
use strings::*;
#[cfg(all(windows, not(windows_slim_errors)))]
mod bstr;
mod error;
pub use error::*;
mod hresult;
pub use hresult::HRESULT;
mod bool;
pub use bool::BOOL;
/// A specialized [`Result`] type that provides Windows error information.
pub type Result<T> = core::result::Result<T, Error>;

29
vendor/windows-result/src/strings.rs vendored Normal file
View File

@@ -0,0 +1,29 @@
use super::*;
pub struct HeapString(pub *mut u16);
impl Default for HeapString {
fn default() -> Self {
Self(core::ptr::null_mut())
}
}
impl Drop for HeapString {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe {
HeapFree(GetProcessHeap(), 0, self.0 as _);
}
}
}
}
pub fn wide_trim_end(mut wide: &[u16]) -> &[u16] {
while let Some(last) = wide.last() {
match last {
32 | 9..=13 => wide = &wide[..wide.len() - 1],
_ => break,
}
}
wide
}