sync_fn_ptr

Macro sync_fn_ptr 

Source
macro_rules! sync_fn_ptr {
    ($sig:ty, $var:expr) => { ... };
}
Expand description

This macro creates a Function Pointer that is Send+Sync and guaranteed to have the same representation in memory as a raw function pointer would. (Meaning its size is usize)

ยงExample


use std::ffi::c_void;
use sync_ptr::sync_fn_ptr;
use sync_ptr::SyncFnPtr;

extern "C" fn test_function() -> u64 {
    123456u64
}

fn some_other_function() {
    let test_fn_ptr = sync_fn_ptr!(extern "C" fn() -> u64, test_function);
    //Type of test_fn_ptr is SendFnPtr<extern "C" fn() -> u64>
    std::thread::spawn(move || {
        assert_eq!(test_fn_ptr(), test_function());
    }).join().unwrap();
}