Skip to content

Commit

Permalink
Merge pull request #258 from DenisBiryukov91/feature/platform-random
Browse files Browse the repository at this point in the history
Add support for random-generating primitives
  • Loading branch information
milyin committed Mar 5, 2024
2 parents 06b2dba + 680deb0 commit fcf2198
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@

pub use synchronization::*;
mod synchronization;

pub use random::*;
mod random;
34 changes: 34 additions & 0 deletions src/platform/random.rs
Original file line number Diff line number Diff line change
@@ -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::<u8>()
}

#[no_mangle]
pub extern "C" fn z_random_u16() -> u16 {
random::<u16>()
}

#[no_mangle]
pub extern "C" fn z_random_u32() -> u32 {
random::<u32>()
}

#[no_mangle]
pub extern "C" fn z_random_u64() -> u64 {
random::<u64>()
}

#[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);
}

0 comments on commit fcf2198

Please sign in to comment.