đŹThis is a nightly-only experimental API. (
proc_macro_internals
#27812)Expand description
Abstraction for creating fn
pointers from any callable that effectively
has the equivalent of implementing Default
, even if the compiler neither
provides Default
nor allows reifying closures (i.e. creating fn
pointers)
other than those with absolutely no captures.
More specifically, for a closure-like type to be âeffectively Default
â:
- it must be a ZST (zero-sized type): no information contained within, so
that
Default
âs return value (if it were implemented) is unambiguous - it must be
Copy
: no captured âunique ZST tokensâ or any other similar types that would make duplicating values at will unsound- combined with the ZST requirement, this confers a kind of âtelecopyâ
ability: similar to
Copy
, but without keeping the value around, and instead âreconstructingâ it (a noop given itâs a ZST) when needed
- combined with the ZST requirement, this confers a kind of âtelecopyâ
ability: similar to
- it must be provably inhabited: no captured uninhabited types or any
other types that cannot be constructed by the user of this abstraction
- the proof is a value of the closure-like type itself, in a sense the
âseedâ for the âtelecopyâ process made possible by ZST +
Copy
- this requirement is the only reason an abstraction limited to a specific
usecase is required: ZST +
Copy
can be checked with at worst a panic at the âattempted::default()
callâ time, but that doesnât guarantee that the value can be soundly created, and attempting to use the typical âproof ZST tokenâ approach leads yet again to having a ZST +Copy
type that is not proof of anything without a value (i.e. isomorphic to a newtype of the type itâs trying to prove the inhabitation of)
- the proof is a value of the closure-like type itself, in a sense the
âseedâ for the âtelecopyâ process made possible by ZST +
A more flexible (and safer) solution to the general problem could exist once
const
-generic parameters can have type parameters in their types:
â
extern "C" fn ffi_wrapper<
A, R,
F: Fn(A) -> R,
const f: F, // <-- this `const`-generic is not yet allowed
>(arg: A) -> R {
f(arg)
}
Macros§
- define_
reify_ đfunctions Experimental
Functions§
- _reify_
to_ đextern_ c_ fn_ unary Experimental - reify_
to_ đextern_ c_ fn_ hrt_ bridge Experimental