Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for random-generating primitives #258

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading