1 | use super::*; |
2 | |
3 | /// A 32-bit value representing boolean values and returned by some functions to indicate success or failure. |
4 | #[must_use ] |
5 | #[repr (transparent)] |
6 | #[derive (Clone, Copy, Debug, PartialEq, Eq, Default)] |
7 | pub struct BOOL(pub i32); |
8 | |
9 | impl BOOL { |
10 | /// Converts the [`BOOL`] to a [`prim@bool`] value. |
11 | #[inline ] |
12 | pub fn as_bool(self) -> bool { |
13 | self.0 != 0 |
14 | } |
15 | |
16 | /// Converts the [`BOOL`] to [`Result<()>`][Result<_>]. |
17 | #[inline ] |
18 | pub fn ok(self) -> Result<()> { |
19 | if self.as_bool() { |
20 | Ok(()) |
21 | } else { |
22 | Err(Error::from_win32()) |
23 | } |
24 | } |
25 | |
26 | /// Asserts that `self` is a success code. |
27 | #[inline ] |
28 | #[track_caller ] |
29 | pub fn unwrap(self) { |
30 | self.ok().unwrap(); |
31 | } |
32 | |
33 | /// Asserts that `self` is a success code using the given panic message. |
34 | #[inline ] |
35 | #[track_caller ] |
36 | pub fn expect(self, msg: &str) { |
37 | self.ok().expect(msg); |
38 | } |
39 | } |
40 | |
41 | impl From<BOOL> for bool { |
42 | fn from(value: BOOL) -> Self { |
43 | value.as_bool() |
44 | } |
45 | } |
46 | |
47 | impl From<&BOOL> for bool { |
48 | fn from(value: &BOOL) -> Self { |
49 | value.as_bool() |
50 | } |
51 | } |
52 | |
53 | impl From<bool> for BOOL { |
54 | fn from(value: bool) -> Self { |
55 | if value { |
56 | Self(1) |
57 | } else { |
58 | Self(0) |
59 | } |
60 | } |
61 | } |
62 | |
63 | impl From<&bool> for BOOL { |
64 | fn from(value: &bool) -> Self { |
65 | (*value).into() |
66 | } |
67 | } |
68 | |
69 | impl PartialEq<bool> for BOOL { |
70 | fn eq(&self, other: &bool) -> bool { |
71 | self.as_bool() == *other |
72 | } |
73 | } |
74 | |
75 | impl PartialEq<BOOL> for bool { |
76 | fn eq(&self, other: &BOOL) -> bool { |
77 | *self == other.as_bool() |
78 | } |
79 | } |
80 | |
81 | impl core::ops::Not for BOOL { |
82 | type Output = Self; |
83 | fn not(self) -> Self::Output { |
84 | if self.as_bool() { |
85 | Self(0) |
86 | } else { |
87 | Self(1) |
88 | } |
89 | } |
90 | } |
91 | |