safer_ffi/closure/
mod.rs

1#![cfg_attr(rustfmt, rustfmt::skip)]
2//! Closures with a `#[repr(C)]` layout (inlined vtable),
3//! up to 9 function arguments.
4//!
5//! Simplified for lighter documentation, but the actual `struct` definitions
6//! and impls range up to `...DynFn...9`.
7//!
8//! ## Examples
9//!
10//! ### FFI-safe `Box<dyn FnMut()>`
11//!
12/*!  - ```rust
13    use ::safer_ffi::prelude::*;
14
15    let mut captured = String::from("…");
16    let ffi_safe: repr_c::Box<dyn Send + FnMut()> =
17        Box::new(move || {
18            captured.push('!');
19            println!("{}", captured);
20        })
21        .into()
22    ;
23
24    fn assert_ffi_safe (_: &impl ReprC)
25    {}
26    assert_ffi_safe(&ffi_safe);
27    ``` */
28//!
29//! ### FFI-safe `Arc<dyn Fn()>`
30//!
31/*!  - ```rust
32    use ::{
33        safer_ffi::{
34            prelude::*,
35        },
36        std::{
37            sync::Arc,
38        },
39    };
40
41    let captured = String::from("…");
42    let ffi_safe: repr_c::Arc<dyn Send + Sync + Fn()> =
43        Arc::new(move || {
44            println!("{}", captured);
45        })
46        .into()
47    ;
48
49    fn assert_ffi_safe (_: &impl ReprC)
50    {}
51    assert_ffi_safe(&ffi_safe);
52    ``` */
53//!
54
55cfg_alloc! {
56    pub mod arc;
57    pub mod boxed;
58    #[doc(no_inline)]
59    pub use self::{
60        arc::{ArcDynFn0, ArcDynFn1},
61        boxed::{BoxDynFnMut0, BoxDynFnMut1},
62    };
63    #[cfg(not(docs))]
64    #[doc(no_inline)]
65    pub use self::{
66        arc::{
67            ArcDynFn2, ArcDynFn3, ArcDynFn4, ArcDynFn5,
68            ArcDynFn6, ArcDynFn7, ArcDynFn8, ArcDynFn9,
69        },
70        boxed::{
71            BoxDynFnMut2, BoxDynFnMut3, BoxDynFnMut4, BoxDynFnMut5,
72            BoxDynFnMut6, BoxDynFnMut7, BoxDynFnMut8, BoxDynFnMut9,
73        },
74    };
75}
76
77pub mod borrowed;
78
79#[doc(no_inline)]
80pub use borrowed::{RefDynFnMut0, RefDynFnMut1};
81#[cfg(not(docs))]
82#[doc(no_inline)]
83pub use borrowed::{
84    RefDynFnMut2, RefDynFnMut3, RefDynFnMut4, RefDynFnMut5,
85    RefDynFnMut6, RefDynFnMut7, RefDynFnMut8, RefDynFnMut9,
86};