use alloc::boxed::Box;
use core::ffi::c_void;
pub struct CallbackHandle<F> {
boxed: Box<F>,
}
impl<F> CallbackHandle<F> {
pub fn new(callback: F) -> Self {
CallbackHandle {
boxed: Box::new(callback),
}
}
pub fn baton(&self) -> *mut c_void {
&*self.boxed as *const F as *mut c_void
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_callback_handle() {
let mut counter = 0;
let callback = move || {
counter += 1;
counter
};
let handle = CallbackHandle::new(callback);
assert!(!handle.baton().is_null());
}
}