Skip to main content

use_logic/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4//! Logic utilities for `RustUse`.
5
6pub mod prelude;
7
8/// Returns the material implication `left -> right`.
9#[must_use]
10pub const fn implication(left: bool, right: bool) -> bool {
11    !left || right
12}
13
14/// Returns whether two boolean values are logically equivalent.
15#[must_use]
16pub const fn equivalence(left: bool, right: bool) -> bool {
17    left == right
18}
19
20/// Returns the exclusive-or of two boolean values.
21#[must_use]
22pub const fn exclusive_or(left: bool, right: bool) -> bool {
23    left ^ right
24}
25
26/// Returns the NAND of two boolean values.
27#[must_use]
28pub const fn nand(left: bool, right: bool) -> bool {
29    !(left && right)
30}
31
32/// Returns the NOR of two boolean values.
33#[must_use]
34pub const fn nor(left: bool, right: bool) -> bool {
35    !(left || right)
36}
37
38/// Returns whether at least two of three inputs are `true`.
39#[must_use]
40pub const fn majority(first: bool, second: bool, third: bool) -> bool {
41    (first && (second || third)) || (second && third)
42}
43
44#[cfg(test)]
45mod tests {
46    use super::{equivalence, exclusive_or, implication, majority, nand, nor};
47
48    #[test]
49    fn computes_binary_boolean_helpers() {
50        assert!(implication(false, false));
51        assert!(implication(false, true));
52        assert!(implication(true, true));
53        assert!(!implication(true, false));
54
55        assert!(equivalence(true, true));
56        assert!(equivalence(false, false));
57        assert!(!equivalence(true, false));
58
59        assert!(exclusive_or(true, false));
60        assert!(exclusive_or(false, true));
61        assert!(!exclusive_or(true, true));
62        assert!(!exclusive_or(false, false));
63
64        assert!(!nand(true, true));
65        assert!(nand(true, false));
66
67        assert!(nor(false, false));
68        assert!(!nor(true, false));
69    }
70
71    #[test]
72    fn computes_majority_truths() {
73        assert!(majority(true, true, false));
74        assert!(majority(true, false, true));
75        assert!(majority(false, true, true));
76        assert!(majority(true, true, true));
77        assert!(!majority(true, false, false));
78        assert!(!majority(false, false, false));
79    }
80}