bitwisetools/
lib.rs

1//! `bitwisetools` allows you to encode and decode bitwise fields easily
2#[cfg(test)]
3mod tests {
4    use super::*;
5    #[test]
6    fn decode_encode() {
7
8        let reference = vec!["thing1", "thing2", "thing3"];
9        assert_eq!(5, encode_bitwise(reference.clone(), decode_bitwise(reference, 5)));
10    }
11    #[test]
12    fn encode() {
13        let reference = vec!["thing1", "thing2", "thing3"];
14        let actual = vec!["thing1", "thing3"];
15        assert_eq!(5, encode_bitwise(reference, actual))
16    }
17}
18/// Decodes a bitwise field of type `u32`
19///
20/// # Examples
21/// ```
22/// let reference = vec!["thing1", "thing2", "thing3"];
23/// let array = bitwisetools::decode_bitwise(reference, 5);
24/// assert_eq!(vec!["thing1", "thing3"], array);
25/// ```  
26pub fn decode_bitwise<T>(i: T, n: u32) -> T
27where
28    T: IntoIterator + std::iter::FromIterator<<T as std::iter::IntoIterator>::Item> + std::fmt::Debug,
29{
30    i.into_iter().enumerate().filter(|(i, _x)| (1 << i) == (n & (1 << i))).map(|(_i, x)| x).collect::<T>()
31}
32
33/// Encodes an array (of any iterable type) to a bitwise field
34///
35/// # Panics
36/// There are no guarantees around arguments with duplicate items
37/// 
38/// # Examples
39/// ```
40/// let reference = vec!["thing1", "thing2", "thing3"];
41/// let actual = vec!["thing1", "thing3"];
42/// let field = bitwisetools::encode_bitwise(reference, actual); 
43/// assert_eq!(5, field)
44/// ```
45 
46pub fn encode_bitwise<T>(r: T, w: T) -> u32
47where
48    T: IntoIterator,
49    <T as std::iter::IntoIterator>::IntoIter: std::clone::Clone,
50    <T as std::iter::IntoIterator>::Item: std::cmp::PartialEq,
51    <T as std::iter::IntoIterator>::Item: std::fmt::Debug
52{
53    let ri = r.into_iter();
54    let mut n = 0;
55    w.into_iter().for_each(|x| n += 1 << ri.clone().position(|v| v == x).ok_or(format!("item \"{:?}\" not found in reference collection", x)).unwrap());
56    n as u32
57}
58