Macro qed::lossless_cast_u32_to_usize

source ·
macro_rules! lossless_cast_u32_to_usize {
    ($num:expr) => { ... };
}
Expand description

Cast a u32 to usize at runtime with a compile time assert that the cast is lossless and will not overflow.

This macro emits a compile time assertion that usize has at least as many bits as u32.

§Examples

#[derive(Default)]
struct SymbolTable {
    next: u32,
    table: [&'static str; 32],
}

impl SymbolTable {
    pub fn intern(&mut self, symbol: &'static str) -> u32 {
        let id = self.next;
        let idx = qed::lossless_cast_u32_to_usize!(id);
        self.table[idx] = symbol;
        self.next += 1;
        id
    }
}

let mut table = SymbolTable::default();
assert_eq!(table.intern("end of proof"), 0);
assert_eq!(table.intern("∎"), 1);

This macro requires a u32 as its argument:

qed::lossless_cast_u32_to_usize!(0_i32);