Provides wrappers around closures which allows them to be called through context-free unsafe bare functions.
Context-free bare functions are not needed very often, as properly designed C APIs typically allow the user to specify an opaque pointer to a context object which will be provided to the function pointer. However, this is not always the case, and may be impossible in less common scenarios, e.g. function hooking for game modding/hacking.
Features
The crate comes with the following feature flags:
no_std: Makes the crate compatible with#![no_std]. A dependency onallocandspinis still required.bundled_jit_alloc: Provides a global JIT allocator through thejit-allocator2crate. This is enabled by default.custom_jit_alloc: Allows providing a global JIT allocator through theglobal_jit_alloc!macro. This is incompatible withbundled_jit_alloc.proc_macros: Provides thebare_hrtbproc macro which is necessary for creating bare functions with signatures that involve higher-kinded lifetimes (i.e.for<'a, ...>statements), as well as thebare_dynproc macro for writingBareFn*types of boxed closures (i.e.Box<dyn Fn()>) more concisely.unstable: Enable the use of unstable Rust features for aspects of the crate that benefit from them without causing any API breaks. Unstable features that can cause breaking changes when enabled are gated separately.tuple_trait: Adds acore::marker::Tuplebound onFnPtr::Args. This allows downstream crates to easily integrate the library with closure-related nightly features such asunboxed_closuresandfn_traits. Also enables theunstablefeature.c_variadic: Adds partial (no invocation throughcall)FnPtrandFn*Thunkimplementations for variadic functions.full: Enablesbundled_jit_allocandproc_macrosfeatures.
Examples
Passing a closure to a C API taking a contextless function pointer:
use ;
// Imagine we have an foreign C API for reigstering and unregistering some callback function.
// Notably, the API does not let the user provide a context object to the callback.
unsafe extern "C"
unsafe extern "C"