Open koenichiwa opened 11 months ago
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);
});
}
}
Hi I had a question regarding the safety of
remove_alias
. The safety explanation is a bit hard for me to understand. Especially this part:I kinda think you mean that it's unsafe to have multiple chuncks pointing to overlapping parts of the same data, but I'm not sure. Could you help me understand it a bit better?
In my project I need a fixed size array of nibbles, which is one of the reasons I'm using bit_vec in my project. I tried to make a simple example of how I use it:
Is this safe? Is this an XY problem and should I use a different method?