chanced / navajo

A cryptographic toolkit for Rust
Apache License 2.0
1 stars 0 forks source link

navajo: Add 16-bit and 128-bit support in rand::Rng::usize #51

Open chanced opened 1 year ago

chanced commented 1 year ago
    pub fn usize(&self) -> Result<usize, RandomError> {
        // future-proofing
        #[cfg(target_pointer_width = "128")]
        {
            let mut dst = [0; 16];
            fill(&mut dst)?;
            Ok(usize::from_ne_bytes(dst))
        }
        #[cfg(target_pointer_width = "64")]
        {
            let mut dst = [0; 8];
            fill(&mut dst)?;
            Ok(usize::from_ne_bytes(dst))
        }
        #[cfg(target_pointer_width = "32")]
        {
            let mut dst = [0; 4];
            fill(&mut dst)?;
            Ok(usize::from_ne_bytes(dst))
        }
        #[cfg(target_pointer_width = "16")]
        {
            let mut dst = [0; 2];
            fill(&mut dst)?;
            Ok(usize::from_ne_bytes(dst))
        }
        #[cfg(all(
            not(target_pointer_width = "128"),
            not(target_pointer_width = "64"),
            not(target_pointer_width = "32"),
            not(target_pointer_width = "16"),
        ))]
        {
            compile_error!("Unsupported target_pointer_width")
        }
    }