Align to byte boundary by applying operation to bits before the next byte boundary. This function handles non-byte-aligned operations by processing bits from the current position up to the next byte boundary, while preserving all other bits in the byte. # Arguments `op` - Unary operation to apply `buffer` - The mutable buffer to modify `offset_in_bits` - Starting bit offset (not byte-aligned)
(buffer: &mut [u8], op: &mut F, offset_in_bits: usize)
| 450 | /// * `buffer` - The mutable buffer to modify |
| 451 | /// * `offset_in_bits` - Starting bit offset (not byte-aligned) |
| 452 | fn align_to_byte<F>(buffer: &mut [u8], op: &mut F, offset_in_bits: usize) |
| 453 | where |
| 454 | F: FnMut(u64) -> u64, |
| 455 | { |
| 456 | let byte_offset = offset_in_bits / 8; |
| 457 | let bit_offset = offset_in_bits % 8; |
| 458 | |
| 459 | // 1. read the first byte from the buffer |
| 460 | let first_byte: u8 = buffer[byte_offset]; |
| 461 | |
| 462 | // 2. Shift byte by the bit offset, keeping only the relevant bits |
| 463 | let relevant_first_byte = first_byte >> bit_offset; |
| 464 | |
| 465 | // 3. run the op on the first byte only |
| 466 | let result_first_byte = op(relevant_first_byte as u64) as u8; |
| 467 | |
| 468 | // 4. Shift back the result to the original position |
| 469 | let result_first_byte = result_first_byte << bit_offset; |
| 470 | |
| 471 | // 5. Mask the bits that are outside the relevant bits in the byte |
| 472 | // so the bits until bit_offset are 1 and the rest are 0 |
| 473 | let mask_for_first_bit_offset = (1 << bit_offset) - 1; |
| 474 | |
| 475 | let result_first_byte = |
| 476 | (first_byte & mask_for_first_bit_offset) | (result_first_byte & !mask_for_first_bit_offset); |
| 477 | |
| 478 | // 6. write back the result to the buffer |
| 479 | buffer[byte_offset] = result_first_byte; |
| 480 | } |
| 481 | |
| 482 | /// Centralized structure to handle a mutable u8 slice as a mutable u64 pointer. |
| 483 | /// |
no outgoing calls
no test coverage detected