Passing Rust closures to C

It looks like dealing with unsafe Rust, lifetimes, PhantomData, etc. is far more difficult as I expected. Some links in that matter:

I also stumbled upon NonZero, Unique, and Shared, as the Rustonomicon still mentions Unique in the section on Phantom Data. I searched for Unique :face_with_monocle: and didn't find it in the standard library. There is an old tracking issue #27730 on NonZero, Unique, Shared. The current standard library knows NonNull, which is covariant, but looks like Unique and Shared disappeared?

I feel like I have a lot to learn yet, even if I want to go the 'static way (so I really know what happens and when working with pointers is sound at all). There seem to be a lot of things that can go wrong whenever pointers are involved.

That said, I would like to learn it :grin:.

I believe that the idea of "ephemeral closures" or "weak closures" (not sure about terminology here) can be useful for a couple of usecases. Perhaps it would be best to implement this in a separate crate, which basically allows you to convert a closure with lifetime 'b into a closure with a longer lifetime 'a (or even 'static), whereas when the original closure (or the scope handle) goes out of scope, the closure with lifetime 'a will return an error (or execute a provided "error closure" with lifetime 'a).

If that has been solved, I could limit my own API to expect 'static closures or closures with a lifetime that is as long as the virutal machine exists. An "ephemeral closure" or "weak closure" crate would then allow to convert shorter living closures into longer living ones while specifying what shall happen in case the closure has been called too late (e.g. throw an error inside the VM).