bool32/
lib.rs

1#![no_std]
2#![warn(missing_docs)]
3#![forbid(unsafe_code)]
4#![warn(clippy::missing_const_for_fn)]
5#![warn(clippy::missing_inline_in_public_items)]
6
7//! Provides [Bool32], which is a 32-bit bool-ish type.
8//!
9//! This is primarily of use with FFI, because APIs in C often use an int as
10//! their true/false type.
11
12/// A 32-bit bool-ish type.
13///
14/// This is `repr(transparent)` and wraps a `u32`. When the value is 0 it's
15/// "false" and when the value is non-zero it's "true". The methods of this type
16/// only ever allow the wrapped value to be 0 or 1, but if you make this type
17/// interact with foreign functions they could produce other non-zero values. It
18/// is *not* an absolute invariant of this type that the wrapped value always be
19/// exactly 0 or 1, the wrapped value can be any value.
20///
21/// For readability, the type formats with [Debug](core::fmt::Debug) and
22/// [Display](core::fmt::Display) just like a [bool], showing as either `true`
23/// or `false`.
24#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
25#[repr(transparent)]
26pub struct Bool32(u32);
27impl Bool32 {
28  /// Constant for the `false` value.
29  pub const FALSE: Self = Self::new(false);
30
31  /// Constant for the canonical `true` value.
32  ///
33  /// Note that *any* non-zero wrapped value is considered to be "true", this is
34  /// just the canonical true value (1).
35  pub const TRUE: Self = Self::new(true);
36
37  /// Like `Bool32::from(bool)`, but `const fn`.
38  ///
39  /// ```
40  /// # use bool32::*;
41  /// const F: Bool32 = Bool32::new(false);
42  /// const T: Bool32 = Bool32::new(true);
43  /// assert_eq!(F, Bool32::FALSE);
44  /// assert_eq!(T, Bool32::TRUE);
45  /// ```
46  #[inline]
47  #[must_use]
48  pub const fn new(b: bool) -> Self {
49    Self(b as _)
50  }
51}
52
53impl From<bool> for Bool32 {
54  /// ```
55  /// # use bool32::*;
56  /// assert!(bool::from(Bool32::new(true)));
57  /// assert!(!bool::from(Bool32::new(false)));
58  /// ```
59  #[inline]
60  #[must_use]
61  fn from(value: bool) -> Self {
62    Self(value as _)
63  }
64}
65impl From<Bool32> for bool {
66  /// ```
67  /// # use bool32::*;
68  /// assert_eq!(Bool32::from(true), Bool32::TRUE);
69  /// assert_eq!(Bool32::from(false), Bool32::FALSE);
70  /// ```
71  #[inline]
72  #[must_use]
73  fn from(value: Bool32) -> Self {
74    value.0 != 0
75  }
76}
77
78impl core::fmt::Debug for Bool32 {
79  /// ```
80  /// # use bool32::*;
81  /// assert_eq!(format!("{:?}", true), format!("{:?}", Bool32::TRUE));
82  /// assert_eq!(format!("{:?}", false), format!("{:?}", Bool32::FALSE));
83  /// ```
84  #[inline]
85  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
86    core::fmt::Debug::fmt(&bool::from(*self), f)
87  }
88}
89impl core::fmt::Display for Bool32 {
90  /// ```
91  /// # use bool32::*;
92  /// assert_eq!(format!("{}", true), format!("{}", Bool32::TRUE));
93  /// assert_eq!(format!("{}", false), format!("{}", Bool32::FALSE));
94  /// ```
95  #[inline]
96  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
97    core::fmt::Display::fmt(&bool::from(*self), f)
98  }
99}