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
// use crate::Result;

pub trait NumHelpers {
    type T;

    fn even_parity(&self) -> bool;
    // fn reverse(&self, width: usize) -> Result<Self::T>;
    // fn chunk(
    //     &self,
    //     chunk_width: usize,
    //     data_width: usize,
    //     chunk_lsb_first: bool,
    //     data_lsb_first: bool
    // ) -> crate::Result<Vec<Self::T>>;
}

impl NumHelpers for u32 {
    type T = Self;

    fn even_parity(&self) -> bool {
        if self.count_ones() % 2 == 0 {
            false
        } else {
            true
        }
    }

    // fn reverse(&self, width: usize) -> Result<Self::T> {
    //     Ok(self.reverse_bits())
    // }

    // fn chunk(
    //     &self,
    //     chunk_width: usize,
    //     data_width: usize,
    //     chunk_lsb_first: bool,
    //     data_lsb_first: bool
    // ) -> Result<Vec<Self::T>> {
    //     let mut data = self.clone();
    //     let retn = vec!();
    //     let mask = (1 << chunk_width - 1) as Self;
    //     for i in 0..(data_width/chunk_width) {
    //         retn.push(data & mask);
    //         data >> chunk_width;
    //     }
    //     if data_width % chunk_width != 0 {
    //         retn.push(data);
    //     }
    //     Ok(retn)
    // }
}