I have a function that takes a fixed size buffer as parameter like a 32 byte digest. This data might be buried somewhere in a bigger buffer.
I could make the function parameter a slice
&[u8] but I am not entirely happy with that solution:
- It does not communicate that the parameter has to have exactly 32 bytes
- I have to check the size dynamically in the function or let it panic if the function is used in a wrong way
I could also take a reference to an array as parameter
&[u8; 32] and then convert a slice with try_into()
- Now when I put in a slice where the size is known at compile time I have a lot of noise around this function parameter (
&buf[4..36].try_into().unwrap()) altough there is no chance this could ever panic.
What do you recommend? Is there a better solution?