Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ffi::c_void;
- mod sys {
- use super::*;
- pub type ScopedFunc = unsafe extern "C" fn(*mut c_void);
- pub unsafe fn scoped(callback: Option<ScopedFunc>, user_data: *mut c_void) {
- println!("Doing something fancy in C++");
- (callback.unwrap())(user_data);
- println!("Done executing callback.");
- }
- }
- struct Scope;
- impl Scope {
- pub fn scoped<F>(mut func: F)
- where
- F: FnMut(&Scope),
- {
- unsafe {
- sys::scoped(
- Some(sure::<F>),
- &mut func as *mut _ as *mut std::ffi::c_void,
- );
- }
- }
- }
- unsafe extern "C" fn sure<T>(foo: *mut std::ffi::c_void)
- where
- T: FnMut(&Scope),
- {
- let foo = foo as *mut T;
- (*foo)(&Scope);
- }
- fn main() {
- Scope::scoped(|_| {
- println!("hello from callback");
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement