Skip to main content

windows_result/
rpc_status.rs

1use super::*;
2
3/// An error or status code value returned by some operating system functions.
4#[repr(transparent)]
5#[derive(Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
6#[must_use]
7pub struct RPC_STATUS(pub i32);
8
9impl RPC_STATUS {
10    /// Returns [`true`] if `self` is a success code.
11    #[inline]
12    pub const fn is_ok(self) -> bool {
13        self.0 == 0
14    }
15
16    /// Returns [`true`] if `self` is a failure code.
17    #[inline]
18    pub const fn is_err(self) -> bool {
19        !self.is_ok()
20    }
21
22    /// Maps an RPC error code to an HRESULT value.
23    #[inline]
24    pub const fn to_hresult(self) -> HRESULT {
25        WIN32_ERROR(self.0 as u32).to_hresult()
26    }
27
28    /// Converts the [`RPC_STATUS`] to [`Result<()>`][Result<_>].
29    #[inline]
30    pub fn ok(self) -> Result<()> {
31        self.to_hresult().ok()
32    }
33}
34
35impl From<RPC_STATUS> for HRESULT {
36    fn from(value: RPC_STATUS) -> Self {
37        value.to_hresult()
38    }
39}
40
41impl From<RPC_STATUS> for Error {
42    fn from(value: RPC_STATUS) -> Self {
43        value.to_hresult().into()
44    }
45}
46
47impl core::fmt::Display for RPC_STATUS {
48    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
49        f.write_fmt(format_args!("{:#010X}", self.0))
50    }
51}
52
53impl core::fmt::Debug for RPC_STATUS {
54    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
55        f.write_fmt(format_args!("RPC_STATUS({self})"))
56    }
57}