Announcing Rust 1.46.0

14 Likes

Friendly reminder that the standback crate contains some stabilized APIs from the standard library as extension traits (mostly), and is usable back to Rust 1.31!

Example of const fn. The lookup table for CRC-32 computation is generated at compile time.

pub mod crc {
    const POLYNOMIAL: u32 = 0xedb88320;

    const fn generate_lookup_table() -> [u32; 256] {
        let mut lookup = [0; 256];
        let mut i = 0;
        while i < lookup.len() {
            let mut crc = i as u32;
            let mut j = 0;
            while j < 8 {
                let a = (!(crc & 1)).wrapping_add(1);
                crc = (crc >> 1) ^ (a & POLYNOMIAL);
                j += 1;
            }
            lookup[i] = crc;
            i += 1;
        }
        lookup
    }

    static LOOKUP: [u32; 256] = generate_lookup_table();

    pub fn crc32(data: &[u8], previous: u32) -> u32 {
        let mut crc: u32 = !previous;
        for byte in data {
            let index = usize::from((crc as u8) ^ byte);
            crc = (crc >> 8) ^ LOOKUP[index];
        }
        !crc
    }
}
8 Likes

The const fn improvements are pretty nice, I'm personally waiting to be able to use mutable references in const fn on stable Rust.