Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- let a: Box<dyn Fn(i32)> = Box::new(| x | println!("{:?}", x));
- unsafe {
- use std::slice;
- let ptr = Box::into_raw(Box::new(a));
- let number = ptr as usize;
- let bytes = slice::from_raw_parts(&number as *const _ as *const u8, 8);
- test(bytes);
- };
- }
- #[no_mangle]
- pub unsafe extern "C" fn test(bytes: &[u8]) {
- let number = *(bytes.as_ptr() as *const usize);
- let ptr = number as *mut Box<dyn Fn(i32)>;
- let b = Box::from_raw(ptr);
- b(5);
- }
Add Comment
Please, Sign In to add comment