Giter Club home page Giter Club logo

Comments (1)

koenichiwa avatar koenichiwa commented on August 17, 2024

I think it might've been an XY problem. I'm still hoping for the explanation regarding safety though. Just out of curiosity. Maybe you have an even better solution.

I solved it with something like:

use bitvec::prelude::{BitArray, BitSlice, Lsb0, BitVec};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Nibble(BitArray<u8, Lsb0>);

impl Nibble {
    pub const ZERO: Self = Self(BitArray::ZERO);
    pub const BITS: usize = 4;
    pub const MAX: u8 = 2u8.pow(Self::BITS as u32);

    pub fn new(value: u8) -> Self {
        assert!(value < Self::MAX, "{value} is too high");
        Nibble(BitArray::new(value))
    }

    pub fn as_bitslice(&self) -> &BitSlice<u8, Lsb0> {
        &self.0[0..Self::BITS]
    }
}

impl FromIterator<Nibble> for BitVec<u8, Lsb0> {
    fn from_iter<T: IntoIterator<Item = Nibble>>(iter: T) -> Self {
        iter.into_iter().fold(BitVec::<u8, Lsb0>::EMPTY, |mut acc, nibble| {
            acc.extend_from_bitslice(nibble.as_bitslice());
            acc
        })
    }
}

#[cfg(test)]
mod test {
    use bitvec::prelude::{BitArray,Lsb0, BitVec};
    use super::Nibble;

    #[test]
    fn example() {
        let mut nibbles: [Nibble; Nibble::MAX as usize] = [Nibble::ZERO; Nibble::MAX as usize];
        for index in 0..nibbles.len() {
            nibbles[index] = Nibble::new(index as u8);
        }

        let mut result: BitArray<[u8; 8], Lsb0> = BitArray::ZERO;
        result.copy_from_bitslice(nibbles.iter().copied().collect::<BitVec<u8, Lsb0>>().as_bitslice());
        eprintln!("BitVec contents: {result}");

        result.chunks(4).enumerate().for_each(|(index, chunk)| {
            let vec: BitVec<u8, Lsb0> = BitVec::from_element(index as u8);
            let expected = &vec.as_bitslice()[0..Nibble::BITS];
            assert_eq!(chunk, expected);
        });
    }
}

from bitvec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.