mystor / rust-cpp

Embed C++ directly inside your rust code!
Apache License 2.0
802 stars 44 forks source link

rust! macro in closures #39

Closed ogoffart closed 6 years ago

ogoffart commented 6 years ago

Contains also some other small fixes.

We unfortunately can't just use the same trick as for the raw: The rust! code expands to something like this:

fn containing_function() {
   #[no-mangle] pub extern "C" fn xxxx()  {
        ...
    }
}

This would result in the warning warning: function is marked #[no_mangle], but not exported and would indeed not export the function because it is local to a function context, so we'd get a link error.

Passing the pointer to the functions as parameters also does not work, because we want the function to be accessible even from lambda function within the cpp! macro.

The solution I found is to create an array of callback which are initialized on the first call of the closure.