Eliminating redundant bounds checks on read+write mutable slices

How about this?

use core::cell::Cell;

pub fn why_u_no_optimze(inout: &mut [u8], len: usize, offset: usize) {
    let inout = Cell::from_mut(inout)
        .as_slice_of_cells();
    
    if inout.len() < len || inout.len() < offset || inout.len() < len-offset || offset >= len || len >= u32::MAX as usize || offset > 8 {
        return;
    }

    let out = &inout[..len - offset];
    let inp = &inout[offset..len];

    for i in 0..len-offset {
        out[i].set(inp[i].get() + 1);
    }
}
4 Likes