Skip to main content

topcoat_runtime/surrogate/
_bool.rs

1use ref_cast::RefCast;
2use serde::{Deserialize, Serialize};
3
4use crate::{OptionSurrogate, Surrogate, impl_surrogate, impl_surrogate_mut, impl_surrogate_ref};
5
6#[derive(Debug, RefCast, Clone, Copy, Serialize, Deserialize)]
7#[repr(transparent)]
8#[serde(transparent)]
9pub struct BoolSurrogate(bool);
10
11impl BoolSurrogate {
12    #[inline]
13    pub(crate) const fn new(v: bool) -> Self {
14        Self(v)
15    }
16}
17
18impl_surrogate!(bool, BoolSurrogate);
19impl_surrogate_ref!(bool, BoolSurrogate);
20impl_surrogate_mut!(bool, BoolSurrogate);
21
22impl core::ops::Not for BoolSurrogate {
23    type Output = BoolSurrogate;
24
25    #[inline]
26    fn not(self) -> BoolSurrogate {
27        BoolSurrogate(!self.0)
28    }
29}
30
31macro_rules! impl_cmp_op {
32    ($method:ident, $op:tt) => {
33        impl BoolSurrogate {
34            #[inline]
35            #[must_use]
36            pub fn $method(&self, rhs: &BoolSurrogate) -> BoolSurrogate {
37                BoolSurrogate::new(self.0 $op rhs.0)
38            }
39        }
40    };
41}
42
43impl_cmp_op!(eq, ==);
44impl_cmp_op!(ne, !=);
45
46impl BoolSurrogate {
47    #[inline]
48    pub fn then<F, S>(self, f: F) -> OptionSurrogate<S::Real>
49    where
50        F: FnOnce() -> S,
51        S: Surrogate,
52    {
53        OptionSurrogate::new(self.0.then(|| f().into_real()))
54    }
55
56    #[inline]
57    pub fn then_some<S>(self, t: S) -> OptionSurrogate<S::Real>
58    where
59        S: Surrogate,
60    {
61        OptionSurrogate::new(self.0.then_some(t.into_real()))
62    }
63}
64
65impl std::fmt::Display for BoolSurrogate {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        self.0.fmt(f)
68    }
69}