Currently my code looks like this
let mut f = File::open(filepath).unwrap();
let mut read_bytes2 = |mut f:&File|{
let mut bytes2 = [0u8; 2];
f.read(&mut bytes2);
bytes2
};
let mut read_bytes4 = |mut f:&File|{
let mut bytes4 = [0u8; 4];
f.read(&mut bytes4);
bytes4
};
let a = i16::from_le_bytes(read_bytes2(f.borrow_mut()));
let b = i32::from_ne_bytes(read_bytes4(f.borrow_mut()));
It compiles and works but, basically those two cloures look the same it just the size of the array which is different. Is there an elegant way that I can pass the size of the array into the clousre without running into the error "doesn't have a size known at compile-time".
Because obviously, this throws the error above
let mut read_bytes = |mut f:&File, size:u8|{
let mut bytes4 = [0u8; size];
f.read(&mut bytes4);
bytes4
};
let a = i16::from_le_bytes(read_bytes(f.borrow_mut(), 2));
let b = i32::from_ne_bytes(read_bytes(f.borrow_mut(), 4));
Thanks for your answer.
Currently I've not started working with makros but I guess this approach will work.
The generic function approach does not work as const is not supported so far as paramenter of a generich function or a function at all. At least this seems so for me.