use super::*;
use bindings::*;
#[repr(transparent)]
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
#[must_use]
#[allow(non_camel_case_types)]
pub struct HRESULT(pub i32);
impl HRESULT {
#[inline]
pub const fn is_ok(self) -> bool {
self.0 >= 0
}
#[inline]
pub const fn is_err(self) -> bool {
!self.is_ok()
}
#[inline]
#[track_caller]
pub fn unwrap(self) {
assert!(self.is_ok(), "HRESULT 0x{:X}", self.0);
}
#[inline]
pub fn ok(self) -> Result<()> {
if self.is_ok() {
Ok(())
} else {
Err(self.into())
}
}
pub fn and_some<T: Interface>(self, some: Option<T>) -> Result<T> {
if self.is_ok() {
if let Some(result) = some {
Ok(result)
} else {
Err(Error::OK)
}
} else {
Err(Error::from(self))
}
}
#[inline]
pub fn and_then<F, T>(self, op: F) -> Result<T>
where
F: FnOnce() -> T,
{
self.ok()?;
Ok(op())
}
pub unsafe fn from_abi<T: Abi>(self, abi: T::Abi) -> Result<T> {
if self.is_ok() {
T::from_abi(abi)
} else {
Err(Error::from(self))
}
}
pub fn message(&self) -> HSTRING {
let mut message = HeapString(core::ptr::null_mut());
unsafe {
let size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, core::ptr::null(), self.0 as _, 0, PWSTR(core::mem::transmute(&mut message.0)), 0, core::ptr::null_mut());
HSTRING::from_wide(core::slice::from_raw_parts(message.0 as *const u16, size as usize))
}
}
}
unsafe impl Abi for HRESULT {
type Abi = Self;
}
unsafe impl RuntimeType for HRESULT {
const SIGNATURE: ConstBuffer = ConstBuffer::from_slice(b"struct(Windows.Foundation.HResult;i32)");
}
impl ::windows::core::DefaultType for HRESULT {
type DefaultType = Self;
}
impl<T> core::convert::From<Result<T>> for HRESULT {
fn from(result: Result<T>) -> Self {
if let Err(error) = result {
return error.into();
}
HRESULT(0)
}
}
struct HeapString(*mut u16);
impl Drop for HeapString {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe {
heap_free(self.0 as _);
}
}
}
}