Guest User

Untitled

a guest
Oct 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. fn main() {
  2. let a: Box<dyn Fn(i32)> = Box::new(| x | println!("{:?}", x));
  3.  
  4. unsafe {
  5. use std::slice;
  6. let ptr = Box::into_raw(Box::new(a));
  7. let number = ptr as usize;
  8. let bytes = slice::from_raw_parts(&number as *const _ as *const u8, 8);
  9. test(bytes);
  10. };
  11.  
  12. }
  13.  
  14. #[no_mangle]
  15. pub unsafe extern "C" fn test(bytes: &[u8]) {
  16. let number = *(bytes.as_ptr() as *const usize);
  17. let ptr = number as *mut Box<dyn Fn(i32)>;
  18. let b = Box::from_raw(ptr);
  19. b(5);
  20. }
Add Comment
Please, Sign In to add comment