1 //! Abstraction for creating `fn` pointers from any callable that *effectively*
2 //! has the equivalent of implementing `Default`, even if the compiler neither
3 //! provides `Default` nor allows reifying closures (i.e. creating `fn` pointers)
4 //! other than those with absolutely no captures.
6 //! More specifically, for a closure-like type to be "effectively `Default`":
7 //! * it must be a ZST (zero-sized type): no information contained within, so
8 //! that `Default`'s return value (if it were implemented) is unambiguous
9 //! * it must be `Copy`: no captured "unique ZST tokens" or any other similar
10 //! types that would make duplicating values at will unsound
11 //! * combined with the ZST requirement, this confers a kind of "telecopy"
12 //! ability: similar to `Copy`, but without keeping the value around, and
13 //! instead "reconstructing" it (a noop given it's a ZST) when needed
14 //! * it must be *provably* inhabited: no captured uninhabited types or any
15 //! other types that cannot be constructed by the user of this abstraction
16 //! * the proof is a value of the closure-like type itself, in a sense the
17 //! "seed" for the "telecopy" process made possible by ZST + `Copy`
18 //! * this requirement is the only reason an abstraction limited to a specific
19 //! usecase is required: ZST + `Copy` can be checked with *at worst* a panic
20 //! at the "attempted `::default()` call" time, but that doesn't guarantee
21 //! that the value can be soundly created, and attempting to use the typical
22 //! "proof ZST token" approach leads yet again to having a ZST + `Copy` type
23 //! that is not proof of anything without a value (i.e. isomorphic to a
24 //! newtype of the type it's trying to prove the inhabitation of)
26 //! A more flexible (and safer) solution to the general problem could exist once
27 //! `const`-generic parameters can have type parameters in their types:
29 //! ```rust,ignore (needs future const-generics)
30 //! extern "C" fn ffi_wrapper<
33 //! const f: F, // <-- this `const`-generic is not yet allowed
41 // FIXME(eddyb) this could be `trait` impls except for the `const fn` requirement.
42 macro_rules! define_reify_functions {
44 fn $name:ident $(<$($param:ident),*>)?
45 for $(extern $abi:tt)? fn($($arg:ident: $arg_ty:ty),*) -> $ret_ty:ty;
49 F: Fn($($arg_ty),*) -> $ret_ty + Copy
50 >(f: F) -> $(extern $abi)? fn($($arg_ty),*) -> $ret_ty {
51 // FIXME(eddyb) describe the `F` type (e.g. via `type_name::<F>`) once panic
52 // formatting becomes possible in `const fn`.
53 assert!(mem::size_of::<F>() == 0, "selfless_reify: closure must be zero-sized");
55 $(extern $abi)? fn wrapper<
57 F: Fn($($arg_ty),*) -> $ret_ty + Copy
58 >($($arg: $arg_ty),*) -> $ret_ty {
60 // SAFETY: `F` satisfies all criteria for "out of thin air"
61 // reconstructability (see module-level doc comment).
62 mem::MaybeUninit::<F>::uninit().assume_init()
75 define_reify_functions! {
76 fn _reify_to_extern_c_fn_unary<A, R> for extern "C" fn(arg: A) -> R;
78 // HACK(eddyb) this abstraction is used with `for<'a> fn(BridgeConfig<'a>)
79 // -> T` but that doesn't work with just `reify_to_extern_c_fn_unary`
80 // because of the `fn` pointer type being "higher-ranked" (i.e. the
82 // FIXME(eddyb) try to remove the lifetime from `BridgeConfig`, that'd help.
83 fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: super::BridgeConfig<'_>) -> R;