diff --git a/src/custom.rs b/src/custom.rs index 35771f15..b6efc40c 100644 --- a/src/custom.rs +++ b/src/custom.rs @@ -76,16 +76,23 @@ 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(), + // This unnamed block (stable since 1.37) prevents seeing __getrandom_custom + const _: () = { + // Make sure the passed function has the type of getrandom::getrandom + type F = fn(&mut [u8]) -> ::core::result::Result<(), $crate::Error>; + const _: F = $crate::getrandom; + + // 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 { + let f: F = $path; + let slice = ::core::slice::from_raw_parts_mut(dest, len); + match f(slice) { + Ok(()) => 0, + Err(e) => e.code().get(), + } } - } + }; }; }