Is there some way to create an array reference to a chunk of a larger data structure? As an example, suppose I write the function
fn from_le(x: &[u8]) -> u16 {
x[0] as u16 + x[1] as u16 *8
}
This function has two disadvantages: it's type does not document to the user that it needs a slice of size two, and at runtime the function needs to bounds check. If instead I write:
fn from_le(x: &[u8; 2]) -> u16 {
x[0] as u16 + x[1] as u16 *8
}
Then both of those limitations are removed, but I force the user to make a copy of a larger u8 array rather than operating in place, since a user cannot do something like:
let data: &[u8] = ..;
let n = from_le(&data[5..7]);
Is there a solution to this challenge? It seems like one ought to be able to create an array reference from a slice () with runtime bounds checking, of course), but I don't see any way to do so in rust.
Any ideas? How can I document to humans and compiler the size of my inputs without forcing users to make copies? For other purposes we would use structs to avoid the need for copies but I've been working with encryption code that really cries for u8 slices or arrays.