So here we're doing a common operation - taking a u32 from 4 bytes, big-endian. This is from a program that analyzes "tcpdump" files, so it has a lot of data to look at. Turning 4 bytes from a vector of bytes into a u32 is supported, but the code is kind of clunky.
fn processacks(&self, acks: &[u8]) -> Result<(),&str> {
for i in (0..acks.len()).step_by(std::mem::size_of::<u32>()) {
let seqnum = &acks[i .. i+4]; // next sequence number
/// In an ACK, sequence numbers also are big-endian.
let sequence = u32::from_be_bytes(seqnum.try_into().unwrap()); // panic here should be impossible since previous statement sets size as 4.
self.processack(sequence); // ACK processor
}
Is there a better way? I'm going to have about a thousand statements like that in machine-generated code that's taking apart packets, so I want that operation to be fast.
Is the optimizer smart enough to detect that a byte array created in the previous statement with [i ++ i+4] has to have a length of 4 and avoid generating the panic?