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

Move __getrandom_custom definition into a const block #344

Merged
merged 2 commits into from Mar 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Expand Up @@ -268,7 +268,7 @@ jobs:
strategy:
matrix:
target: [
x86_64-fuchsia,
x86_64-unknown-fuchsia,
x86_64-unknown-redox,
x86_64-fortanix-unknown-sgx,
]
Expand Down
24 changes: 15 additions & 9 deletions src/custom.rs
Expand Up @@ -76,16 +76,22 @@ use core::{mem::MaybeUninit, num::NonZeroU32};
#[cfg_attr(docsrs, doc(cfg(feature = "custom")))]
macro_rules! register_custom_getrandom {
($path:path) => {
// We use an extern "C" function to get the guarantees of a stable ABI.
#[no_mangle]
extern "C" fn __getrandom_custom(dest: *mut u8, len: usize) -> u32 {
let f: fn(&mut [u8]) -> Result<(), $crate::Error> = $path;
let slice = unsafe { ::core::slice::from_raw_parts_mut(dest, len) };
match f(slice) {
Ok(()) => 0,
Err(e) => e.code().get(),
// TODO(MSRV 1.37): change to unnamed block
const __getrandom_internal: () = {
// We use an extern "C" function to get the guarantees of a stable ABI.
#[no_mangle]
unsafe extern "C" fn __getrandom_custom(dest: *mut u8, len: usize) -> u32 {
// Make sure the passed function has the type of getrandom::getrandom
type F = fn(&mut [u8]) -> ::core::result::Result<(), $crate::Error>;
let _: F = $crate::getrandom;
let f: F = $path;
let slice = ::core::slice::from_raw_parts_mut(dest, len);
match f(slice) {
Ok(()) => 0,
Err(e) => e.code().get(),
}
}
}
};
};
}

Expand Down