Skip to content

Commit

Permalink
feat: callback macro - fn new for creating callback in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
elpiel authored and ralfbiedert committed Sep 13, 2024
1 parent fd745e4 commit b52470e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/src/patterns/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ impl NamedCallback {
/// #[repr(transparent)]
/// pub struct MyCallback(Option<extern "C" fn(FFISlice<u8>) -> u8>);
/// ```
///
/// You can also create the callback from Rust for testing:
///
/// ```
/// use interoptopus::callback;
///
/// callback!(MyCallback() -> u8);
///
/// extern "C" fn my_rust_callback() -> u8 {
/// 42
/// }
///
/// let callback = MyCallback::new(my_rust_callback);
/// assert_eq!(42, callback.call());
/// ```
#[macro_export]
macro_rules! callback {
($name:ident($($param:ident: $ty:ty),*)) => {
Expand All @@ -153,6 +168,11 @@ macro_rules! callback {
pub struct $name(Option<extern "C" fn($($ty),*) -> $rval>);

impl $name {
/// Creates a new instance of the callback using `extern "C" fn`
pub fn new(func: extern "C" fn($($ty),*) -> $rval) -> Self {
Self(Some(func))
}

/// Will call function if it exists, panic otherwise.
pub fn call(&self, $($param: $ty),*) -> $rval {
self.0.expect("Assumed function would exist but it didn't.")($($param),*)
Expand Down

0 comments on commit b52470e

Please sign in to comment.