Guest User

Untitled

a guest
Jun 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. let test_cb = move |n: i32| {
  2. n*2
  3. };
  4. let mut _test_worker = new(test_cb);
  5.  
  6. pub struct Worker {
  7. ptr: WorkerPtr, // Pointer to the C data structure
  8. cb: fn(i32) -> i32, // Callback function
  9. }
  10.  
  11. struct WorkerPtr(NonNull<worker>);
  12. unsafe impl marker::Send for WorkerPtr {}
  13. struct worker_s {
  14. void* rust_object; // Pointer to the Rust data structure
  15. };
  16.  
  17. #[no_mangle]
  18. pub extern fn rust_callback(w: *mut Worker, raw_n: c_int) -> c_int {
  19. let n = raw_n as i32;
  20. println!("rust_callback: {}", n);
  21. unsafe {
  22. let out = (*w).trigger_callback(n);
  23. out as c_int
  24. }
  25. }
  26.  
  27. worker_new: worker = 0x7f9834d000c0
  28. worker_set_rust_object: worker = 0x7f9834d000c0 rust_object = 0x10ca21000
  29. trigger_callback: worker = 0x7f9834d000c0 rust_object = 0x10ca21000
  30. rust_callback: 100
  31. trigger_callback: 100
  32. got: 200
Add Comment
Please, Sign In to add comment