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.
5//!
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)
25//!
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:
28//!
29//! ```rust,ignore (needs future const-generics)
30//! extern "C" fn ffi_wrapper<
31//! A, R,
32//! F: Fn(A) -> R,
33//! const f: F, // <-- this `const`-generic is not yet allowed
34//! >(arg: A) -> R {
35//! f(arg)
36//! }
37//! ```
38
39use std::mem;
40
41// FIXME(eddyb) this could be `trait` impls except for the `const fn` requirement.
42macro_rules! define_reify_functions {
43 ($(
44 fn $name:ident $(<$($param:ident),*>)?
45 for $(extern $abi:tt)? fn($($arg:ident: $arg_ty:ty),*) -> $ret_ty:ty;
46 )+) => {
47 $(pub const fn $name<
48 $($($param,)*)?
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");
54
55 $(extern $abi)? fn wrapper<
56 $($($param,)*)?
57 F: Fn($($arg_ty),*) -> $ret_ty + Copy
58 >($($arg: $arg_ty),*) -> $ret_ty {
59 let f = unsafe {
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()
63 };
64 f($($arg),*)
65 }
66 let _f_proof = f;
67 wrapper::<
68 $($($param,)*)?
69 F
70 >
71 })+
72 }
73}
74
75define_reify_functions! {
76 fn _reify_to_extern_c_fn_unary<A, R> for extern "C" fn(arg: A) -> R;
77
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
81 // `for<'a>` binder).
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;
84}
85