Skip to main content

windows_result/
bool.rs

1use super::*;
2
3/// A 32-bit Windows Boolean value.
4#[must_use]
5#[repr(transparent)]
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
7pub struct BOOL(pub i32);
8
9impl 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_thread())
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
41impl From<BOOL> for bool {
42    fn from(value: BOOL) -> Self {
43        value.as_bool()
44    }
45}
46
47impl From<&BOOL> for bool {
48    fn from(value: &BOOL) -> Self {
49        value.as_bool()
50    }
51}
52
53impl From<bool> for BOOL {
54    fn from(value: bool) -> Self {
55        if value { Self(1) } else { Self(0) }
56    }
57}
58
59impl From<&bool> for BOOL {
60    fn from(value: &bool) -> Self {
61        (*value).into()
62    }
63}
64
65impl PartialEq<bool> for BOOL {
66    fn eq(&self, other: &bool) -> bool {
67        self.as_bool() == *other
68    }
69}
70
71impl PartialEq<BOOL> for bool {
72    fn eq(&self, other: &BOOL) -> bool {
73        *self == other.as_bool()
74    }
75}
76
77impl core::ops::Not for BOOL {
78    type Output = Self;
79    fn not(self) -> Self::Output {
80        if self.as_bool() { Self(0) } else { Self(1) }
81    }
82}