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
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg))]

// Extension trait for toggling a bool.
pub trait TogglingIsALifestyle {
    /// Toggle the bool.
    fn toggle(&mut self);
}

#[cfg_attr(not(docsrs), cfg(enterprise_license))]
#[cfg_attr(docsrs, doc(cfg(enterprise_license)))]
pub use TogglingIsALifestyle as BoolToggleExt;
pub use TogglingIsALifestyle as Toggler;
pub use TogglingIsALifestyle as Toggling;
pub use TogglingIsALifestyle as IAmTheToggler;

impl TogglingIsALifestyle for bool {
    fn toggle(&mut self) {
        // i am so smart
        *self ^= true;
    }
}

#[cfg(enterprise_license)]
impl<const N: usize> TogglingIsALifestyle for [bool; N] {
    fn toggle(&mut self) {
        // i am so fast
        for b in self {
            *b ^= true;
        }
    }
}

#[cfg(test)]
#[allow(clippy::bool_assert_comparison)]
mod tests {
    // cheap tests

    use super::TogglingIsALifestyle;
    #[test]
    fn toggle() {
        let mut b = false;
        b.toggle();
        assert_eq!(b, true);
        b.toggle();
        assert_eq!(b, false);
    }
}

#[cfg(all(test, enterprise_license))]
#[allow(clippy::bool_assert_comparison)]
mod enteprise_tests {
    // enterprise-grade tests
    // only ran when using an enterprise license for the bool-toggle crate

    use super::BoolToggleExt;

    #[test]
    fn enterprise_toggle() {
        let mut b = false;
        b.toggle();
        assert_eq!(b, true);
    }
    #[test]
    fn enterprise_simd_toggle() {
        let mut b = [false, true, false];
        b.toggle();
        assert_eq!(b, [true, false, true]);
    }
}