bitvec icon indicating copy to clipboard operation
bitvec copied to clipboard

`chunks_exact_mut()` occasionally doesn't return the last chunk when `step_by()` is involved

Open justindhales opened this issue 2 years ago • 0 comments
trafficstars

When iterating over a BitSlice using chunks_exact_mut() and step_by() with a step value greater than 1 and the last chunk lands perfectly at the end of the BitSlice, the last chunk is not returned.

let chunk_size = 2usize;
let step_by = 3usize;

let mut indexes = vec![0, 1, 2, 3, 4, 5, 6, 7];
let mut bits = bitvec![0, 0, 0, 1, 1, 0, 1, 1];

assert_eq!(indexes.len(), bits.len());

println!("indexes.chunks_exact()");
for s in indexes.chunks_exact(chunk_size).step_by(step_by) {
    println!("{:?}", s);
}

println!("indexes.chunks_exact_mut()");
for s in indexes.chunks_exact_mut(chunk_size).step_by(step_by) {
    println!("{:?}", s);
}

println!("bits.chunks_exact()");
for s in bits.chunks_exact(chunk_size).step_by(step_by) {
    println!("{}", s);
}

println!("bits.chunks_exact_mut()");
for s in bits.chunks_exact_mut(chunk_size).step_by(step_by) {
    println!("{}", s);
}

This prints

indexes.chunks_exact()
[0, 1]
[6, 7]
indexes.chunks_exact_mut()
[0, 1]
[6, 7]
bits.chunks_exact()
[0, 0]
[1, 1]
bits.chunks_exact_mut()
[0, 0]

A similar error occurs when

let chunk_size = 1usize;
let step_by = 7usize;

but not when

let chunk_size = 1usize;
let step_by = 6usize;

justindhales avatar Jun 19 '23 23:06 justindhales