I want to work with a bytearray as a list of native-endian u128. I have a function:
fn foo(data: &mut [u8]){
for b in data{
*b = some_u8_funct();
}
}
Inside that function I want to iterate every byte of data
with 'steps' of 16 bytes, modifying them. I want to do it without conversion or any additional cost. (I can do invariant check that data.len() % 16 == 0
).
In C I can just cast char*
to long long long*
(or what C have currently for 128 bits), but it sounds wrong on many level in Rust. What is the best/idiomatic way to do this?
Basically, I want something like that:
fn foo(data: &mut [u8]){
if data.len() % 16 !=0 {
painc!("Bad aligment");
}
for my_u128 in data.some_magic_function(){
*my_u128 = some_u128_funct();
}
}
I've tried to use for b in data as &mut [u128]{}
, but Rust said it's not a primitive type for 'as' conversion.