diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index 9bf02fe81..38f4a03ab 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -1909,6 +1909,11 @@ ZENOHC_API struct z_owned_queryable_t z_queryable_null(void); * Constructs the default value for :c:type:`z_query_reply_options_t`. */ ZENOHC_API struct z_queryable_options_t z_queryable_options_default(void); +ZENOHC_API void z_random_fill(void *buf, size_t len); +ZENOHC_API uint16_t z_random_u16(void); +ZENOHC_API uint32_t z_random_u32(void); +ZENOHC_API uint64_t z_random_u64(void); +ZENOHC_API uint8_t z_random_u8(void); /** * Calls the closure. Calling an uninitialized closure is a no-op. */ diff --git a/src/platform/mod.rs b/src/platform/mod.rs index c9f87a9c7..3c42b831f 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -14,3 +14,6 @@ pub use synchronization::*; mod synchronization; + +pub use random::*; +mod random; diff --git a/src/platform/random.rs b/src/platform/random.rs new file mode 100644 index 000000000..d466e7b77 --- /dev/null +++ b/src/platform/random.rs @@ -0,0 +1,34 @@ +use std::slice::from_raw_parts_mut; + +use libc::c_void; +use rand::{random, thread_rng, RngCore}; + +#[no_mangle] +pub extern "C" fn z_random_u8() -> u8 { + random::() +} + +#[no_mangle] +pub extern "C" fn z_random_u16() -> u16 { + random::() +} + +#[no_mangle] +pub extern "C" fn z_random_u32() -> u32 { + random::() +} + +#[no_mangle] +pub extern "C" fn z_random_u64() -> u64 { + random::() +} + +#[no_mangle] +#[allow(clippy::missing_safety_doc)] +pub unsafe extern "C" fn z_random_fill(buf: *mut c_void, len: usize) { + if buf.is_null() || len == 0 { + return; + } + let b: &mut [u8] = from_raw_parts_mut(buf as *mut u8, len); + thread_rng().fill_bytes(b); +}