pub fn set_coap_prng<RNG: TryRngCore + CryptoRng + Send + Sync + 'static>(
rng: RNG,
) -> Result<(), RngError>Expand description
Configures libcoap to use the provided rng for pseudo-random number generation instead of its
default PRNG.
The provided PRNG will be used globally across all contexts.
Important: DO NOT provide an instance of CoapRng to set_coap_prng! This will probably lead to a stack overflow, as CoapRng would recursively call into itself to generate random bytes. This function will try to catch attempts to do so, but cannot reliably check for all possible cases (e.g., if the CoapRng is wrapped in another custom type).
§Errors
May return RngError::GlobalMutexPoisonError if the underlying mutex protecting the RNG is poisoned, i.e. a thread panicked while holding the lock (which should only happen if the previously set RNG panicked).
Will return RngError::CoapRngAsCustomRng if the TryRngCore implementation provided to this function is a known reference to a CoapRng instance (which would cause infinite recursion).
§Example
use rand_core::{CryptoRng, RngCore};
use libcoap_rs::error::RngError;
use libcoap_rs::prng::{coap_prng_try_fill, set_coap_prng};
pub struct NullRng {}
// This implicitly also adds an implementation for TryRngCore, which is the type that is
// actually required by set_coap_rng().
impl RngCore for NullRng {
fn next_u32(&mut self) -> u32 {
0
}
fn next_u64(&mut self) -> u64 {
0
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
dest.fill(0);
}
}
// Obviously, this is just for demonstration purposes and should not actually be done.
impl CryptoRng for NullRng {}
set_coap_prng(NullRng{})?;
let mut token = [1u8; 8];
coap_prng_try_fill(&mut token)?;
assert_eq!(&token, &[0u8; 8]);