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
#[cfg(test)]
#[path = "../../tests/unit/utils/types_test.rs"]
mod types_test;

use std::fmt;
use std::fmt::Formatter;
use std::ops::ControlFlow;

/// Unwraps value from inner state.
pub trait UnwrapValue {
    /// A value type.
    type Value;

    /// Unwraps value from the type.
    fn unwrap_value(self) -> Self::Value;
}

impl<T> UnwrapValue for ControlFlow<T, T> {
    type Value = T;

    fn unwrap_value(self) -> Self::Value {
        match self {
            ControlFlow::Continue(value) => value,
            ControlFlow::Break(value) => value,
        }
    }
}

/// Returns a short name of a type.
pub fn short_type_name<T: ?Sized>() -> &'static str {
    let name = std::any::type_name::<T>();

    name.rsplit_once(':').map(|(_, name)| name).unwrap_or(name)
}

/// A bit array type of fixed size.
pub struct FixedBitArray<const N: usize> {
    data: [u8; N],
}

impl<const N: usize> Default for FixedBitArray<N> {
    fn default() -> Self {
        Self { data: [0; N] }
    }
}

impl<const N: usize> fmt::Binary for FixedBitArray<N> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        for (count, n) in self.data.iter().enumerate() {
            if count != 0 {
                write!(f, " ")?;
            }

            write!(f, "{:08b}", n.reverse_bits())?;
        }

        Ok(())
    }
}

impl<const N: usize> FixedBitArray<N> {
    /// Sets or unsets bit at given index.
    /// Returns false if index is out of range. Otherwise, returns true.
    pub fn set(&mut self, index: usize, value: bool) -> bool {
        if index >= N * 8 {
            return false;
        }

        let byte_index = index / 8;
        let bit_index = index % 8;

        if value {
            self.data[byte_index] |= 1 << bit_index;
        } else {
            self.data[byte_index] &= !(1 << bit_index);
        }

        true
    }

    /// Gets value at given index.
    /// Always returns false if index is out of range.
    pub fn get(&self, index: usize) -> bool {
        if index >= N * 8 {
            return false;
        }

        let byte_index = index / 8;
        let bit_index = index % 8;

        (self.data[byte_index] & (1 << bit_index)) != 0
    }

    /// Replaces existing value with new value returning an old value.
    /// Always returns false if index is out of range.
    pub fn replace(&mut self, index: usize, new_value: bool) -> bool {
        if index >= N * 8 {
            return false;
        }

        let byte_index = index / 8;
        let bit_index = index % 8;
        let shift = 1 << bit_index;

        let old_value = (self.data[byte_index] & shift) != 0;

        if new_value {
            self.data[byte_index] |= shift;
        } else {
            self.data[byte_index] &= !shift;
        }

        old_value
    }
}