winresult_types/
code.rs

1use core::convert::Infallible;
2
3
4
5/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes)\]
6/// ERROR_\* values that aren't HRESULTs (but might be implicitly convertable)
7#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ErrorCode(pub(crate) u16);
8
9impl ErrorCode {
10    #[doc(hidden)] pub const fn from_constant(value: u16) -> Self { Self(value) }
11
12    pub const fn to_u16(self) -> u16 { self.0 }
13    pub const fn to_u32(self) -> u32 { self.0 as _ }
14}
15
16impl From<u16> for ErrorCode { fn from(c: u16) -> Self { Self(c) } }
17impl From<Infallible> for ErrorCode { fn from(i: Infallible) -> Self { match i {} } }
18impl From<ErrorCode> for u16 { fn from(c: ErrorCode) -> Self { c.0 } }
19impl From<ErrorCode> for u32 { fn from(c: ErrorCode) -> Self { c.0.into() } }
20
21impl<O, E: PartialEq<ErrorCode>> PartialEq<ErrorCode> for Result<O, E> {
22    fn eq(&self, other: &ErrorCode) -> bool {
23        match self.as_ref() {
24            Ok(_)   => false,
25            Err(e)  => *e == *other,
26        }
27    }
28}
29
30impl<O, E: PartialEq<ErrorCode>> PartialEq<Result<O, E>> for ErrorCode {
31    fn eq(&self, other: &Result<O, E>) -> bool {
32        match other.as_ref() {
33            Ok(_)   => false,
34            Err(e)  => *e == *self,
35        }
36    }
37}