Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. use std::os::raw::c_void;
  2.  
  3.  
  4.  
  5.  
  6.  
  7. struct Encoder<TCallback>
  8. where
  9. TCallback: Fn(&[u8]),
  10. {
  11. callback: TCallback,
  12. }
  13.  
  14.  
  15.  
  16.  
  17.  
  18. type EncoderType = Encoder<Box<Fn(&[u8])>>;
  19.  
  20. #[no_mangle]
  21. unsafe extern "C" fn encoder_create(
  22. on_data: unsafe extern "C" fn(data: *const u8, length: usize),
  23. instance: *mut *mut c_void,
  24. ) -> bool {
  25. let encoder: EncoderType = Encoder {
  26. callback: move |data| unsafe { (on_data)(data.as_ptr(), data.len()) },
  27. };
  28.  
  29. *instance = Box::into_raw(Box::new(encoder)) as *mut c_void;
  30. true
  31. }
  32.  
  33. #[no_mangle]
  34. pub unsafe extern "C" fn encoder_destroy(instance: *mut c_void) {
  35. if !instance.is_null() {
  36. Box::from_raw(instance as *mut EncoderType);
  37. }
  38. }
  39.  
  40.  
  41.  
  42.  
  43. fn main() {
  44. println!("Hello, world!");
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement