Module safer_ffi::closure

source ·
Expand description

Closures with a #[repr(C)] layout (inlined vtable), up to 9 function arguments.

Simplified for lighter documentation, but the actual struct definitions and impls range up to ...DynFn...9.

Examples

FFI-safe Box<dyn FnMut()>

  • use ::safer_ffi::prelude::*;
    
    let mut captured = String::from("…");
    let ffi_safe: repr_c::Box<dyn Send + FnMut()> =
        Box::new(move || {
            captured.push('!');
            println!("{}", captured);
        })
        .into()
    ;
    
    fn assert_ffi_safe (_: &impl ReprC)
    {}
    assert_ffi_safe(&ffi_safe);

FFI-safe Arc<dyn Fn()>

  • use ::{
        safer_ffi::{
            prelude::*,
        },
        std::{
            sync::Arc,
        },
    };
    
    let captured = String::from("…");
    let ffi_safe: repr_c::Arc<dyn Send + Sync + Fn()> =
        Arc::new(move || {
            println!("{}", captured);
        })
        .into()
    ;
    
    fn assert_ffi_safe (_: &impl ReprC)
    {}
    assert_ffi_safe(&ffi_safe);

Re-exports

Modules

  • arcalloc
    Arc<dyn 'static + Send + Sync + Fn(...) -> _> but with a #[repr(C)] layout (inlined virtual method table).
  • &'lt mut (dyn 'lt + Send + FnMut(...) -> _> but with a #[repr(C)] layout (env ptr + function ptr).
  • boxedalloc
    Box<dyn 'static + Send + FnMut(...) -> _> but with a #[repr(C)] layout (inlined virtual method table).