1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::{Abi, ErrorCode};

/// A 32-bit boolean error code value returned by some Win32 functions.
#[repr(transparent)]
#[derive(Copy, Clone, Default)]
pub struct BOOL(pub i32);

/// A BOOL representing true.
pub const TRUE: BOOL = BOOL(1);

/// A BOOL representing false.
pub const FALSE: BOOL = BOOL(0);

impl BOOL {
    /// Convert the `BOOL` into a `bool`.
    #[inline]
    pub fn as_bool(self) -> bool {
        if self.0 == 0 {
            false
        } else {
            true
        }
    }

    /// Asserts that `self` is a success code.
    #[inline]
    pub fn unwrap(self) {
        self.ok().unwrap();
    }

    /// Expects that `self` is a success code.
    #[inline]
    pub fn expect(self, msg: &str) {
        self.ok().expect(msg);
    }

    /// Converts the `BOOL` to `Result<()>`.
    #[inline]
    pub fn ok(self) -> crate::Result<()> {
        if self.as_bool() {
            Ok(())
        } else {
            Err(ErrorCode::from_thread().into())
        }
    }
}

unsafe impl Abi for BOOL {
    type Abi = Self;
}

impl From<BOOL> for bool {
    fn from(value: BOOL) -> Self {
        value.as_bool()
    }
}

impl From<&BOOL> for bool {
    fn from(value: &BOOL) -> Self {
        value.as_bool()
    }
}

impl From<bool> for BOOL {
    fn from(value: bool) -> Self {
        if value {
            TRUE
        } else {
            FALSE
        }
    }
}

impl From<&bool> for BOOL {
    fn from(value: &bool) -> Self {
        (*value).into()
    }
}

impl std::fmt::Debug for BOOL {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let msg = if self.as_bool() { "TRUE" } else { "FALSE" };
        f.write_str(msg)
    }
}

impl PartialEq<BOOL> for BOOL {
    fn eq(&self, other: &BOOL) -> bool {
        self.as_bool() == other.as_bool()
    }
}

impl PartialEq<bool> for BOOL {
    fn eq(&self, other: &bool) -> bool {
        self.as_bool() == *other
    }
}

impl PartialEq<BOOL> for bool {
    fn eq(&self, other: &BOOL) -> bool {
        *self == other.as_bool()
    }
}

impl std::ops::Not for BOOL {
    type Output = Self;
    fn not(self) -> Self::Output {
        if self.as_bool() {
            FALSE
        } else {
            TRUE
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn comparison() {
        let win_bool: BOOL = TRUE;
        let rust_bool: bool = true;
        assert_eq!(rust_bool, win_bool);

        let win_bool: BOOL = FALSE;
        let rust_bool: bool = false;
        assert_eq!(win_bool, rust_bool);

        let win_bool = BOOL(123);
        let rust_bool: bool = true;
        assert_eq!(rust_bool, win_bool);

        let win_bool = BOOL(-123);
        let rust_bool: bool = true;
        assert_eq!(win_bool, rust_bool);

        let a: BOOL = TRUE;
        let b: BOOL = BOOL(123);
        assert_eq!(a, b);
    }

    #[test]
    fn win_bool_to_rust_bool() {
        let win_bool: BOOL = BOOL(123);
        let rust_bool: bool = win_bool.as_bool();
        assert!(rust_bool);

        let rust_bool: bool = win_bool.into();
        assert!(rust_bool);

        let win_bool_ref: &BOOL = &win_bool;
        let rust_bool: bool = win_bool_ref.as_bool();
        assert!(rust_bool);

        let rust_bool: bool = win_bool_ref.into();
        assert!(rust_bool);
    }

    #[test]
    fn rust_bool_to_win_bool() {
        let rust_bool: bool = true;
        let win_bool: BOOL = rust_bool.into();
        assert_eq!(win_bool, true);

        let rust_bool: &bool = &rust_bool;
        let win_bool: BOOL = rust_bool.into();
        assert_eq!(win_bool, true);

        let rust_bool: bool = false;
        let win_bool: BOOL = rust_bool.into();
        assert_eq!(win_bool, false);

        let rust_bool: &bool = &rust_bool;
        let win_bool: BOOL = rust_bool.into();
        assert_eq!(win_bool, false);
    }

    #[test]
    fn methods() {
        let win_bool: BOOL = BOOL(123);
        assert!(win_bool.as_bool());
        win_bool.unwrap();
        win_bool.expect("test");
        assert!(win_bool.ok().is_ok());

        let win_bool: BOOL = FALSE;
        assert!(!win_bool.as_bool());
        assert!(win_bool.ok().is_err());
    }

    #[test]
    fn format() {
        assert_eq!(format!("{:?}", TRUE), "TRUE");
        assert_eq!(format!("{:?}", FALSE), "FALSE");
        assert_eq!(format!("{:?}", BOOL(123)), "TRUE");
    }

    #[test]
    fn not() {
        let win_bool: BOOL = TRUE;
        let not: BOOL = !win_bool;
        assert!(not != win_bool);
        assert!(not == FALSE);

        let win_bool: BOOL = FALSE;
        let not: BOOL = !win_bool;
        assert!(not != win_bool);
        assert!(not == TRUE);
    }
}