closure-ffi 5.1.2

FFI utility for creating bare function pointers that invoke a closure
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
//! Provides the [`BareFnOnce`], [`BareFnMut`] and [`BareFn`] wrapper types which allow closures to
//! be called through context-free unsafe bare functions.
//!
//! Variants which erase the signature of the bare function (e.g. [`UntypedBareFn`]) are also
//! provided. This can be useful when a type needs to own wrappers for functions of different
//! signatures.
//!
//! # Thread Safety
//!
//! The closure wrapper types provided by this module are (trivially) [`Send`] if and only if both
//! the closure's type-erased storage and the executable memory allocator used are.
//!
//! Canonically, they should also always be [`Sync`], as the invariants required for soundly
//! calling the wrapped closure across threads are encoded in the `unsafe`ness of the
//! bare function pointer returned by [`BareFnOnce::leak`], [`BareFnMut::bare`] and
//! [`BareFn::bare`], and documented on these (safe) functions.
//!
//! However, such a [`Sync`] impl makes it very easy to call the bare function in situations where
//! it is not allowed:
//!
//! ```compile_fail
//! use closure_ffi::BareFn;
//! use std::{cell::Cell, thread};
//! let cell = Cell::new(0);
//! // WARNING: `wrapped` is Sync, but not the closure!
//! let wrapped = BareFn::new_c(move || -> u32 {
//!     let val = cell.get();
//!     cell.set(val + 1);
//!     val
//! });
//! // `wrapped` can be borrowed here at is it Sync. But by `bare()` documentation,
//! // calling the function is unsound as the closure is not Sync!
//! thread::scope(|s| {
//!     s.spawn(|| unsafe { wrapped.bare()() });
//!     s.spawn(|| unsafe { wrapped.bare()() });
//! });
//! ```
//!
//! To help guard against this, [`Sync`] is only implemented:
//! - for [`BareFnOnceAny`]: When the closure is [`Send`]. The user is still responsible for
//!   guarding against repeated calls.
//! - for [`BareFnMutAny`]: When the closure is [`Send`]. The user is still responsible for guarding
//!   against unsynchronized calls.
//! - for [`BareFnAny`]: When the closure is [`Sync`].

use alloc::boxed::Box;
use core::{marker::PhantomData, mem::ManuallyDrop};

#[cfg(feature = "proc_macros")]
#[doc(hidden)]
pub use closure_ffi_proc_macros::bare_hrtb as bare_hrtb_impl;

#[cfg(feature = "global_jit_alloc")]
use crate::jit_alloc::GlobalJitAlloc;
#[allow(unused_imports)]
use crate::{
    arch::AllocatedThunk,
    cc,
    jit_alloc::{JitAlloc, JitAllocError},
    traits::{Any, FnMutThunk, FnOnceThunk, FnPtr, FnThunk, ToBoxedDyn},
};

#[cfg(not(feature = "coverage"))]
#[doc(hidden)]
#[macro_export]
macro_rules! bare_hrtb_inner {
    ($($tokens:tt)*) => {
        $crate::bare_closure::bare_hrtb_impl! { $crate, $($tokens)* }
    };
}

#[cfg(feature = "coverage")]
#[doc(hidden)]
#[macro_export]
macro_rules! bare_hrtb_inner {
    ($($tokens:tt)*) => {
        $crate::bare_closure::bare_hrtb_impl! { #[coverage(off)] $crate, $($tokens)* }
    };
}

/// Declares a wrapper type around a higher-ranked bare function which can be used with [BareFn] and
/// friends.
///
/// <div class="warning">
///
/// Note that if using the `coverage` crate feature, the code generated by this macro will require
/// the [`coverage_attribute`](https://doc.rust-lang.org/beta/unstable-book/language-features/coverage-attribute.html)
/// unstable Rust feature to be enabled.
///
/// </div>
///
/// # Usage
///
/// The syntax is equivalent to that of a type alias:
/// ```
/// #![cfg_attr(feature = "coverage", feature(coverage_attribute))]
///
/// closure_ffi::bare_hrtb! {
///     type MyFn = for<'a> extern "C" fn(&'a str) -> &'a u32;
/// }
/// ```
///
/// `unsafe` is automatically added to the function signature if absent. Generic type parameters and
/// bounds can be added to the alias:
/// ```
/// #![cfg_attr(feature = "coverage", feature(coverage_attribute))]
///
/// closure_ffi::bare_hrtb! {
///     type MyFn<T: Clone + 'static> = for<'a> extern "C" fn(&'a T) -> T;
/// }
/// ```
///
/// A calling convention marker type is also created under the alias named suffixed by `_CC`, with
/// the same visibility.
///
/// # Limitations
///
/// ## Maximum of 3 independent lifetimes
///
/// Higher-ranked bare functions with more than 3 independent bound lifetimes are not supported.
/// For example, the following will not compile:
///
/// ```compile_fail
/// #![cfg_attr(feature = "coverage", feature(coverage_attribute))]
///
/// closure_ffi::bare_hrtb! {
///     type MyFn = extern "C" fn<'a, 'b, 'c, 'd>(&'a u8, &'b u8) -> (&'c u8, &'d u8);
/// }
/// ```
///
/// ## No implicit higher-ranked lifetimes
///
/// Furthermore, implicit higher-ranked lifetimes are not supported in the signature. So
/// ```compile_fail
/// #![cfg_attr(feature = "coverage", feature(coverage_attribute))]
///
/// closure_ffi::bare_hrtb! {
///     type MyFn = extern "C" fn(&u8) -> &u8;
/// }
/// ```
///
/// must instead be written as
/// ```
/// #![cfg_attr(feature = "coverage", feature(coverage_attribute))]
///
/// closure_ffi::bare_hrtb! {
///     type MyFn = for<'a> extern "C" fn(&'a u8) -> &'a u8;
/// }
/// ```
///
/// ## Static bound requirements on generic parameters
///
/// Generic parameters whose lifetime is implicitly lower-bounded by one of the `for<...>` lifetimes
/// must be `'static`. For example, the following will not compile without a `'static` bound on `T`
/// and `U`, but no bound is required on `V`:
///
/// ```compile_fail
/// #![cfg_attr(feature = "coverage", feature(coverage_attribute))]
///
/// struct RefMut<'a, T>(&'a mut T);
///
/// closure_ffi::bare_hrtb! {
///     type MyFn<T, U, V> = for<'a> extern "C" fn(&'a T, RefMut<'a, U>) -> V;
/// }
/// ```
///
/// # Why use this macro?
///
/// Higher-ranked bare functions are not automatically supported by `closure-ffi` as
/// - It is not possible to blanket implement traits for them;
/// - Even if it was possible, type inference via closure annotations would become impossible when
///   references are involved.
///
/// Hence the following won't compile:
/// ```compile_fail
/// #![cfg_attr(feature = "coverage", feature(coverage_attribute))]
///
/// use closure_ffi::BareFn;
///
/// fn take_higher_rank_fn(
///     bare_fn: unsafe extern "C" fn(&Option<u32>) -> Option<&u32>
/// ) {}
///
/// let bare_closure = BareFn::new_c(|opt: &Option<u32>| opt.as_ref());
/// take_higher_rank_fn(bare_closure.bare());
/// ```
///
/// However, using the type generated by this macro as the bare function type, we can get it to
/// work:
/// ```
/// #![cfg_attr(feature = "coverage", feature(coverage_attribute))]
///
/// use closure_ffi::{BareFn, bare_hrtb};
///
/// bare_hrtb! {
///     type MyFn = for<'a> extern "C" fn(&'a Option<u32>) -> Option<&'a u32>;
/// }
///
/// fn take_higher_rank_fn(
///     bare_fn: unsafe extern "C" fn(&Option<u32>) -> Option<&u32>
/// ) {}
///
/// // alternatively, BareFn::with_cc(MyFn_CC, |opt| opt.as_ref())
/// let bare_closure = BareFn::<MyFn>::new(|opt| opt.as_ref());
/// take_higher_rank_fn(bare_closure.bare().into());
/// ```
#[cfg(feature = "proc_macros")]
#[macro_export]
macro_rules! bare_hrtb {
    ($($tokens:tt)*) => {
        $crate::bare_hrtb_inner! { $($tokens)* }
    };
}

#[cfg(feature = "proc_macros")]
pub use bare_hrtb;

#[allow(unused_macros)]
macro_rules! cc_shorthand {
    ($fn_name:ident, $trait_ident:ident, $cc_ty:ty, $cc_name:literal $(,$cfg:meta)?) => {
        $(#[cfg(any($cfg, doc))])?
        #[doc = "Create a bare function thunk using the "]
        #[doc = $cc_name]
        #[doc = "calling convention for `fun`."]
        ///
        /// The W^X memory required is allocated using the global JIT allocator.
        #[inline]
        pub fn $fn_name<F>(fun: F) -> Self
        where
            F: ToBoxedDyn<S>,
            ($cc_ty, F): $trait_ident<B>,
        {
            Self::with_cc(<$cc_ty>::default(), fun)
        }
    };
}

macro_rules! cc_shorthand_in {
    ($fn_name:ident, $trait_ident:ident, $cc_ty:ty, $cc_name:literal $(,$cfg:meta)?) => {
        $(#[cfg(any($cfg, doc))])?
        #[doc = "Create a bare function thunk using the "]
        #[doc = $cc_name]
        #[doc = "calling convention for `fun`."]
        ///
        /// The W^X memory required is allocated using the provided JIT allocator.
        #[inline]
        pub fn $fn_name<F>(fun: F, jit_alloc: A) -> Self
        where
            F: ToBoxedDyn<S>,
            ($cc_ty, F): $trait_ident<B>,
        {
            Self::with_cc_in(<$cc_ty>::default(), fun, jit_alloc)
        }
    };
}

macro_rules! bare_closure_impl {
    (
        ty_name: $ty_name:ident,
        erased_ty_name: $erased_ty_name:ident,
        non_sync_alias: $non_sync_alias:ident,
        sync_alias: $sync_alias:ident,
        sync_bounds: ($($sync_bounds: path),*),
        sync_alias_bound: $sync_alias_bound: ty,
        trait_ident: $trait_ident:ident,
        thunk_template: $thunk_template:ident,
        bare_toggle: $bare_toggle:meta,
        bare_receiver: $bare_receiver:ty,
        fn_trait_doc: $fn_trait_doc:literal,
        ty_name_doc: $ty_name_doc:literal,
        with_cc_doc: $with_cc_doc:literal,
        with_cc_in_doc: $with_cc_in_doc:literal,
        try_with_cc_in_doc: $try_with_cc_in_doc:literal,
        non_sync_alias_doc: $non_sync_alias_doc:literal,
        sync_alias_doc: $sync_alias_doc:literal,
        sync_alias_bound_doc: $sync_alias_bound_doc:literal,
        safety_doc: $safety_doc:literal
    ) => {
        #[cfg(feature = "global_jit_alloc")]
        #[cfg_attr(docsrs, doc(cfg(all())))]
        /// Type-erased wrapper around a
        #[doc = $fn_trait_doc]
        /// closure which exposes a pointer to a bare function thunk.
        ///
        /// This type cannot be directly constructed; it must be produced from a
        #[doc = $ty_name_doc]
        /// through the `into_untyped` method or the [`Into`] trait.
        ///
        /// # Type parameters
        /// - `S`: The dynamically-sized type to use to type-erase the closure. Use this to enforce
        ///   lifetime bounds and marker traits which the closure must satisfy, e.g. `S = dyn Send +
        ///   'a`. Without the `unstable` feature, this is limited to [`dyn Any`](Any) and
        ///   combinations of [`Send`] and [`Sync`] marker types.
        /// - `A`: The [`JitAlloc`] implementation used to allocate and free executable memory.
        ///
        /// # Layout
        #[doc = $ty_name_doc]
        /// is `#[repr(transparent)]` with this type, so it is safe to transmute back and forth
        /// provided the type parameters match.
        #[allow(dead_code)]
        pub struct $erased_ty_name<S: ?Sized, A: JitAlloc = GlobalJitAlloc> {
            thunk: AllocatedThunk<A>,
            // We can't directly own the closure, even through an UnsafeCell.
            // Otherwise, holding a reference to a BareFnMut while the bare function is
            // being called would be UB! So we reclaim the pointer in the Drop impl.
            storage: *mut S,
        }

        // We copy the documentation to this type, since IDEs will often fetch it from here for
        // pop-up documentation if neither feature is on
        #[cfg(not(feature = "global_jit_alloc"))]
        /// Type-erased wrapper around a
        #[doc = $fn_trait_doc]
        /// closure which exposes a pointer to a bare function thunk.
        ///
        /// This type cannot be directly constructed; it must be produced from a
        #[doc = $ty_name_doc]
        /// through the `into_untyped` method or the [`Into`] trait.
        ///
        /// # Type parameters
        /// - `S`: The dynamically-sized type to use to type-erase the closure. Use this to enforce
        ///   lifetime bounds and marker traits which the closure must satisfy, e.g. `S = dyn Send +
        ///   'a`. Without the `unstable` feature, this is limited to [`dyn Any`](Any) and
        ///   combinations of [`Send`] and [`Sync`] marker types.
        /// - `A`: The [`JitAlloc`] implementation used to allocate and free executable memory.
        ///
        /// # Layout
        #[doc = $ty_name_doc]
        /// is `#[repr(transparent)]` with this type, so it is safe to transmute back and forth
        /// provided the type parameters match.
        #[allow(dead_code)]
        pub struct $erased_ty_name<S: ?Sized, A: JitAlloc> {
            thunk: AllocatedThunk<A>,
            storage: *mut S,
        }

        // SAFETY: S and A can be moved to other threads
        unsafe impl<S: ?Sized + Send, A: JitAlloc + Send> Send for $erased_ty_name<S, A> {}
        // SAFETY: See macro invocation
        unsafe impl<S: $($sync_bounds+)* ?Sized, A: JitAlloc> Sync for $erased_ty_name<S, A> {}

        impl<S: ?Sized, A: JitAlloc> $erased_ty_name<S, A> {
            #[$bare_toggle]
            /// Return a type-erased pointer to the bare function thunk wrapping the closure.
            ///
            /// # Safety
            /// While this method is safe, using the returned pointer is very much not. In
            /// particular, the only safe thing to do with it is casting it to the exact bare
            /// function signature it had before erasure. Even then, it must not be called when:
            /// - The lifetime of `self` has expired, or `self` has been dropped.
            #[doc = $safety_doc]
            #[inline]
            pub fn bare(self: $bare_receiver) -> *const () {
                self.thunk.thunk_ptr()
            }

            /// Leak the underlying closure, returning the unsafe bare function pointer that invokes
            /// it.
            ///
            /// `self` must be `'static` for this method to be called.
            ///
            /// # Safety
            /// While this method is safe, using the returned pointer is very much not. In
            /// particular, the only safe thing to do with it is casting it to the exact bare
            /// function signature it had before erasure. Even then, it must not be called when:
            #[doc = $safety_doc]
            #[inline]
            pub fn leak(self) -> *const ()
            where
                Self: 'static,
            {
                ManuallyDrop::new(self).thunk.thunk_ptr()
            }

            /// Weaken the bounds of the type-erased storage.
            ///
            /// For example, a [`UntypedBareFn<dyn Send + Sync>`] may be upcast into a [`UntypedBareFn<dyn Send>`].
            pub fn upcast<U: ?Sized>(self) -> $erased_ty_name<U, A>
                where PhantomData<S>: ToBoxedDyn<U>
            {
                // SAFETY: This is fine on stable since all trait objects implementing `ToBoxedDyn`
                // only have the destructor in their vtable.
                //
                // With the `unstable` feature, it's sketchy as a boxed `dyn Subtrait` can `Unsize` into
                // a `dyn Supertrait` when `Subtrait: Supertrait`. However, the undocumented layout of
                // trait object vtables makes the destructor the first entry, so it's fine for now
                // (and the foreseeable future).
                unsafe { core::mem::transmute_copy(&ManuallyDrop::new(self)) }
            }
        }

        impl<B: FnPtr, S: ?Sized, U: ?Sized, A: JitAlloc> From<$ty_name<B, S, A>> for $erased_ty_name<U, A>
            where PhantomData<S>: ToBoxedDyn<U>
        {
            fn from(value: $ty_name<B, S, A>) -> Self {
                value.into_untyped().upcast()
            }
        }

        impl<S: ?Sized, A: JitAlloc> Drop for $erased_ty_name<S, A> {
            fn drop(&mut self) {
                // Free the closure
                // SAFETY:
                // - The caller of `bare()` promised not to call through the thunk after
                // the lifetime of self expires, so no borrow on closure exists
                drop(unsafe { Box::from_raw(self.storage) })
            }
        }

        #[cfg(feature = "global_jit_alloc")]
        #[cfg_attr(docsrs, doc(cfg(all())))]
        /// Wrapper around a
        #[doc = $fn_trait_doc]
        /// closure which exposes a bare function thunk that can invoke it without
        /// additional arguments.
        ///
        /// # Note
        /// This is a generic implementation which allows customizing the closure's
        /// type erased storage, which allows enforcing trait bounds like `Send` and `Sync` when
        /// needed. However, this plays poorly with type inference. Consider using the
        #[doc = $non_sync_alias_doc]
        /// and
        #[doc = $sync_alias_doc]
        /// type aliases for the common cases of [`S = dyn Any + 'a`](Any) (no thread safety constraints)
        /// and
        #[doc = $sync_alias_bound_doc]
        /// (minimum required to safely store/call the closure from other threads), respectively.
        ///
        /// # Type parameters
        /// - `B`: The bare function pointer to expose the closure as. For higher-kinded bare
        ///   function pointers, you will need to use the [`bare_hrtb`] macro to define a wrapper
        ///   type.
        /// - `S`: The dynamically-sized type to use to type-erase the closure. Use this to enforce
        ///   lifetime bounds and marker traits which the closure must satisfy, e.g. `S = dyn Send +
        ///   'a`. Without the `unstable` feature, this is limited to [`dyn Any`](Any) and
        ///   combinations of [`Send`] and [`Sync`] marker types.
        /// - `A`: The [`JitAlloc`] implementation used to allocate and free executable memory.
        #[allow(dead_code)]
        #[repr(transparent)]
        pub struct $ty_name<B: FnPtr, S: ?Sized, A: JitAlloc = GlobalJitAlloc> {
            untyped: $erased_ty_name<S, A>,
            phantom: PhantomData<B>,
        }

        // We copy the documentation to this type, since IDEs will often fetch it from here for
        // pop-up documentation of neither feature is on
        #[cfg(not(feature = "global_jit_alloc"))]
        /// Wrapper around a
        #[doc = $fn_trait_doc]
        /// closure which exposes a bare function thunk that can invoke it without
        /// additional arguments.
        ///
        /// This is a generic implementation which allows customizing the closure's
        /// type erased storage, which allows enforcing trait bounds like `Send` and `Sync` when
        /// needed. However, this plays poorly with type inference. Consider using the
        #[doc = $non_sync_alias_doc]
        /// and
        #[doc = $sync_alias_doc]
        /// type aliases for the common cases of [`S = dyn Any + 'a`](Any) (no thread safety constraints)
        /// and
        #[doc = $sync_alias_bound_doc]
        /// (minimum required to safely store/call the closure from other threads), respectively.
        ///
        /// # Type parameters
        /// - `B`: The bare function pointer to expose the closure as. For higher-kinded bare
        ///   function pointers, you will need to use the [`bare_hrtb`] macro to define a wrapper
        ///   type.
        /// - `S`: The dynamically-sized type to use to type-erase the closure. Use this to enforce
        ///   lifetime bounds and marker traits which the closure must satisfy, e.g. `S = dyn Send +
        ///   'a`. Without the `unstable` feature, this is limited to [`dyn Any`](Any) and
        ///   combinations of [`Send`] and [`Sync`] marker types.
        /// - `A`: The [`JitAlloc`] implementation used to allocate and free executable memory.
        #[allow(dead_code)]
        pub struct $ty_name<B: FnPtr, S: ?Sized, A: JitAlloc> {
            untyped: $erased_ty_name<S, A>,
            phantom: PhantomData<B>,
        }

        // Split the impl blocks so that the relevant functions appear first in docs
        #[cfg(feature = "global_jit_alloc")]
        impl<B: FnPtr, S: ?Sized> $ty_name<B, S, GlobalJitAlloc> {
            /// Wraps `fun`, producing a bare function of signature `B`.
            ///
            /// This constructor is best used when the type of `B` is already known from an existing
            /// type annotation. If you want to infer `B` from the closure arguments and a calling
            /// convention, consider using
            #[doc = $with_cc_doc]
            /// or the `new_*` suffixed constructors instead.
            ///
            /// The W^X memory required is allocated using the global JIT allocator.
            #[inline]
            pub fn new<F>(fun: F) -> Self
            where
                F: ToBoxedDyn<S>,
                (B::CC, F): $trait_ident<B>,
            {
                Self::new_in(fun, Default::default())
            }

            /// Wraps `fun`, producing a bare function with calling convention `cconv`.
            ///
            /// The W^X memory required is allocated using the global JIT allocator.
            #[inline]
            pub fn with_cc<CC, F>(cconv: CC, fun: F) -> Self
            where
                F: ToBoxedDyn<S>,
                (CC, F): $trait_ident<B>,
            {
                Self::with_cc_in(cconv, fun, Default::default())
            }

            /// Create a
            #[doc = $ty_name_doc]
            /// directly from a
            #[doc = concat!("[`", stringify!($trait_ident), "`].")]
            ///
            /// The W^X memory required is allocated using the global JIT allocator.
            pub fn with_thunk<T>(thunk: T) -> Self where T: $trait_ident<B> + ToBoxedDyn<S>
            {
                Self::with_thunk_in(thunk, Default::default())
            }
        }

        impl<B: FnPtr, S: ?Sized, A: JitAlloc> $ty_name<B, S, A> {
            #[$bare_toggle]
            /// Return a bare function pointer that invokes the underlying closure.
            ///
            /// # Safety
            /// While this method is safe, the returned function pointer is not. In particular, it
            /// must not be called when:
            /// - The lifetime of `self` has expired, or `self` has been dropped.
            #[doc = $safety_doc]
            #[inline]
            pub fn bare(self: $bare_receiver) -> B {
                // SAFETY: B is a bare function pointer
                unsafe { B::from_ptr(self.untyped.bare()) }
            }

            /// Leak the underlying closure, returning the unsafe bare function pointer that invokes
            /// it.
            ///
            /// `self` must be `'static` for this method to be called.
            ///
            /// # Safety
            /// While this method is safe, the returned function pointer is not. In particular, it
            /// must not be called when:
            #[doc = $safety_doc]
            #[inline]
            pub fn leak(self) -> B
            where
                Self: 'static,
            {
                // SAFETY: B is a bare function pointer
                unsafe { B::from_ptr(self.untyped.leak()) }
            }

            /// Erase the signature type from this
            #[doc = $ty_name_doc]
            ///
            /// The returned value also exposes the `bare` and `leak` functions.
            /// However, they now return untyped pointers.
            pub fn into_untyped(self) -> $erased_ty_name<S, A> {
                self.untyped
            }

            /// Weaken the bounds of the type-erased storage.
            ///
            /// For example, a [`BareFnAny<B, dyn Send + Sync>`] may be upcast into a [`BareFnAny<B, dyn Send>`].
            pub fn upcast<U: ?Sized>(self) -> $ty_name<B, U, A>
                where PhantomData<S>: ToBoxedDyn<U>,
            {
                // SAFETY: This is fine on stable since all trait objects implementing `ToBoxedDyn`
                // only have the destructor in their vtable.
                //
                // With the `unstable` feature, it's sketchy as a boxed `dyn Subtrait` can `Unsize` into
                // a `dyn Supertrait` when `Subtrait: Supertrait`. However, the undocumented layout of
                // trait object vtables makes the destructor the first entry, so it's fine for now
                // (and the foreseeable future).
                unsafe { core::mem::transmute_copy(&ManuallyDrop::new(self)) }
            }

            /// Wraps `fun`, producing a bare function with calling convention `cconv`.
            ///
            /// Uses the provided JIT allocator to allocate the W^X memory used to create the thunk.
            #[allow(unused_variables)]
            pub fn try_with_cc_in<CC, F>(
                cconv: CC,
                fun: F,
                jit_alloc: A,
            ) -> Result<Self, JitAllocError>
            where
                F: ToBoxedDyn<S>,
                (CC, F): $trait_ident<B>,
            {
                let storage = Box::into_raw(F::to_boxed_unsize(fun));

                // SAFETY:
                // - thunk_template pointer obtained from the correct source
                // - `closure` is a valid pointer to `fun`
                let thunk = unsafe {
                    AllocatedThunk::new(
                        <(CC, F)>::$thunk_template,
                        storage as *const _, size_of::<F>(),
                        jit_alloc
                    )?
                };
                Ok(Self {
                    untyped: $erased_ty_name {
                        thunk,
                        storage
                    },
                    phantom: PhantomData,
                })
            }

            /// Wraps `fun`, producing a bare function with calling convention `cconv`.
            ///
            /// Uses `jit_alloc` to allocate the W^X memory used to create the thunk.
            ///
            /// # Panics
            /// If the provided JIT allocator fails to allocate memory. For a non-panicking
            /// version, see
            #[doc = $try_with_cc_in_doc]
            #[allow(unused_variables)]
            #[inline]
            pub fn with_cc_in<CC, F>(cconv: CC, fun: F, jit_alloc: A) -> Self
            where
                F: ToBoxedDyn<S>,
                (CC, F): $trait_ident<B>,
            {
                Self::try_with_cc_in(cconv, fun, jit_alloc).unwrap()
            }

            /// Wraps `fun`, producing a bare function with signature `B`.
            ///
            /// Uses `jit_alloc` to allocate the W^X memory used to create the thunk.
            ///
            /// This constructor is best used when the type of `B` is already known from an existing
            /// type annotation. If you want to infer `B` from the closure arguments and a calling
            /// convention, consider using
            #[doc = $with_cc_in_doc]
            /// instead.
            ///
            /// # Panics
            /// If the provided JIT allocator fails to allocate memory. For a non-panicking
            /// version, see
            #[doc = $try_with_cc_in_doc]
            #[inline]
            pub fn new_in<F>(fun: F, jit_alloc: A) -> Self
            where
                F: ToBoxedDyn<S>,
                (B::CC, F): $trait_ident<B>,
            {
                Self::with_cc_in(B::CC::default(), fun, jit_alloc)
            }

            /// Create a
            #[doc = $ty_name_doc]
            /// directly from a
            #[doc = concat!("[`", stringify!($trait_ident), "`].")]
            ///
            /// Uses `jit_alloc` to allocate the W^X memory used to create the thunk.
            pub fn try_with_thunk_in<T>(thunk: T, jit_alloc: A) -> Result<Self, JitAllocError>
            where T: $trait_ident<B> + ToBoxedDyn<S>
            {
                // SAFETY: All implementors of `Fn*Thunk` are #[repr(transparent)] with the closure
                let storage = Box::into_raw(T::to_boxed_unsize(thunk));

                // SAFETY:
                // - thunk_template pointer obtained from the correct source
                // - `closure` is a valid pointer to `fun`
                // - `size_of::<T>()` equals the size of the closure
                let thunk = unsafe {
                    AllocatedThunk::new(
                        T::$thunk_template,
                        storage as *const _, size_of::<T>(),
                        jit_alloc
                    )?
                };
                Ok(Self {
                    untyped: $erased_ty_name {
                        thunk,
                        storage
                    },
                    phantom: PhantomData,
                })
            }

            /// Create a
            #[doc = $ty_name_doc]
            /// directly from a
            #[doc = concat!("[`", stringify!($trait_ident), "`].")]
            ///
            /// Uses `jit_alloc` to allocate the W^X memory used to create the thunk.
            ///
            /// # Panics
            /// If the provided JIT allocator fails to allocate memory. For a non-panicking
            /// version, see [`Self::try_with_thunk_in`].
            #[inline]
            pub fn with_thunk_in<T>(thunk: T, jit_alloc: A) -> Self
            where T: $trait_ident<B> + ToBoxedDyn<S>
            {
                Self::try_with_thunk_in(thunk, jit_alloc).unwrap()
            }
        }

        #[cfg(feature = "global_jit_alloc")]
        impl<B: FnPtr, S: ?Sized> $ty_name<B, S, GlobalJitAlloc> {
            cc_shorthand!(new_rust, $trait_ident, cc::Rust, "Rust");

            cc_shorthand!(new_c, $trait_ident, cc::C, "C");

            cc_shorthand!(new_system, $trait_ident, cc::System, "system");

            cc_shorthand!(new_efiabi, $trait_ident, cc::Efiapi, "efiapi");

            cc_shorthand!(
                new_sysv64,
                $trait_ident,
                cc::Sysv64,
                "sysv64",
                all(not(windows), target_arch = "x86_64")
            );

            cc_shorthand!(
                new_aapcs,
                $trait_ident,
                cc::Aapcs,
                "aapcs",
                any(doc, target_arch = "arm")
            );

            cc_shorthand!(
                new_fastcall,
                $trait_ident,
                cc::Fastcall,
                "fastcall",
                all(windows, target_arch = "x86")
            );

            cc_shorthand!(
                new_stdcall,
                $trait_ident,
                cc::Stdcall,
                "stdcall",
                all(windows, target_arch = "x86")
            );

            cc_shorthand!(
                new_cdecl,
                $trait_ident,
                cc::Cdecl,
                "cdecl",
                all(windows, target_arch = "x86")
            );

            cc_shorthand!(
                new_thiscall,
                $trait_ident,
                cc::Thiscall,
                "thiscall",
                all(windows, target_arch = "x86")
            );

            cc_shorthand!(
                new_win64,
                $trait_ident,
                cc::Win64,
                "win64",
                all(windows, target_arch = "x86_64")
            );

            cc_shorthand!(
                new_variadic,
                $trait_ident,
                cc::Variadic,
                "`C` variadic",
                feature = "c_variadic"
            );
        }

        impl<B: FnPtr, S: ?Sized, A: JitAlloc> $ty_name<B, S, A> {
            cc_shorthand_in!(new_rust_in, $trait_ident, cc::Rust, "Rust");

            cc_shorthand_in!(new_c_in, $trait_ident, cc::C, "C");

            cc_shorthand_in!(new_system_in, $trait_ident, cc::System, "system");

            cc_shorthand_in!(new_efiabi_in, $trait_ident, cc::Efiapi, "efiapi");

            cc_shorthand_in!(
                new_sysv64_in,
                $trait_ident,
                cc::Sysv64,
                "sysv64",
                all(not(windows), target_arch = "x86_64")
            );

            cc_shorthand_in!(
                new_aapcs_in,
                $trait_ident,
                cc::Aapcs,
                "aapcs",
                any(doc, target_arch = "arm")
            );

            cc_shorthand_in!(
                new_fastcall_in,
                $trait_ident,
                cc::Fastcall,
                "fastcall",
                all(windows, target_arch = "x86")
            );

            cc_shorthand_in!(
                new_stdcall_in,
                $trait_ident,
                cc::Stdcall,
                "stdcall",
                all(windows, target_arch = "x86")
            );

            cc_shorthand_in!(
                new_cdecl_in,
                $trait_ident,
                cc::Cdecl,
                "cdecl",
                all(windows, target_arch = "x86")
            );

            cc_shorthand_in!(
                new_thiscall_in,
                $trait_ident,
                cc::Thiscall,
                "thiscall",
                all(windows, target_arch = "x86")
            );

            cc_shorthand_in!(
                new_win64_in,
                $trait_ident,
                cc::Win64,
                "win64",
                all(windows, target_arch = "x86_64")
            );

            cc_shorthand_in!(
                new_variadic_in,
                $trait_ident,
                cc::Variadic,
                "`C` variadic",
                feature = "c_variadic"
            );
        }

        #[cfg(feature = "global_jit_alloc")]
        #[cfg_attr(docsrs, doc(cfg(all())))]
        /// Wrapper around a
        #[doc = $fn_trait_doc]
        /// closure which exposes a bare function thunk that can invoke it without
        /// additional arguments.
        ///
        /// This is a type alias only. Additional details and methods are described on the
        #[doc = $ty_name_doc]
        /// type.
        pub type $non_sync_alias<'a, B, A = GlobalJitAlloc> = $ty_name<B, dyn Any + 'a, A>;

        #[cfg(not(feature = "global_jit_alloc"))]
        /// Wrapper around a
        #[doc = $fn_trait_doc]
        /// closure which exposes a bare function thunk that can invoke it without
        /// additional arguments.
        ///
        /// This is a type alias only. Additional details and methods are described on the
        #[doc = $ty_name_doc]
        /// type.
        pub type $non_sync_alias<'a, B, A> = $ty_name<B, dyn Any + 'a, A>;

        #[cfg(feature = "global_jit_alloc")]
        #[cfg_attr(docsrs, doc(cfg(all())))]
        /// Wrapper around a
        #[doc = $fn_trait_doc]
        /// closure which exposes a bare function thunk that can invoke it without
        /// additional arguments.
        ///
        /// Unlike
        #[doc = $non_sync_alias_doc]
        /// this type enforces the correct combination of [`Send`] and [`Sync`] on the
        /// closure so that it is safe to both store and call from other threads.
        ///
        /// This is a type alias only. Additional details and methods are described on the
        #[doc = $ty_name_doc]
        /// type.
        pub type $sync_alias<'a, B, A = GlobalJitAlloc> = $ty_name<B, $sync_alias_bound, A>;

        #[cfg(not(feature = "global_jit_alloc"))]
        /// Wrapper around a
        #[doc = $fn_trait_doc]
        /// closure which exposes a bare function thunk that can invoke it without
        /// additional arguments.
        ///
        /// Unlike
        #[doc = $non_sync_alias_doc]
        /// this type enforces the correct combination of [`Send`] and [`Sync`] on the
        /// closure so that it is safe to both store and call from other threads.
        ///
        /// This is a type alias only. Additional details and methods are described on the
        #[doc = $ty_name_doc]
        /// type.
        pub type $sync_alias<'a, B, A> = $ty_name<B, $sync_alias_bound, A>;
    };
}

// TODO:
// BareFnOnce still needs work.
// In particular, to avoid leaks we need to have the compiler generated thunk
// call `release` on the allocator after it's done running, then drop the allocator.
// Then, to avoid double frees we need `bare` to be taken by value.
//
// At the moment, we simply force leaking for `BareFnOnce` by omitting `bare()`.

bare_closure_impl!(
    ty_name: BareFnOnceAny,
    erased_ty_name: UntypedBareFnOnce,
    non_sync_alias: BareFnOnce,
    sync_alias: BareFnOnceSync,
    sync_bounds: (Send), // FnOnce only needs to be Send
    sync_alias_bound: dyn Send + 'a,
    trait_ident: FnOnceThunk,
    thunk_template: THUNK_TEMPLATE_ONCE,
    bare_toggle: cfg(any()),
    bare_receiver: Self,
    fn_trait_doc: "[`FnOnce`]",
    ty_name_doc: "[`BareFnOnceAny`]",
    with_cc_doc: "[`with_cc`](BareFnOnceAny::with_cc)",
    with_cc_in_doc: "[`with_cc_in`](BareFnOnceAny::with_cc_in)",
    try_with_cc_in_doc: "[`try_with_cc_in`](BareFnOnceAny::try_with_cc_in)",
    non_sync_alias_doc: "[`BareFnOnce`]",
    sync_alias_doc: "[`BareFnOnceSync`]",
    sync_alias_bound_doc: "[`S = dyn Send + 'a`](Send)",
    safety_doc: "- The function has been called before.\n
- The closure is not `Send`, if calling from a different thread than the current one."
);

bare_closure_impl!(
    ty_name: BareFnMutAny,
    erased_ty_name: UntypedBareFnMut,
    non_sync_alias: BareFnMut,
    sync_alias: BareFnMutSync,
    sync_bounds: (Send), // For FnMut we only need Send to make synchronized calls safe
    sync_alias_bound: dyn Send + 'a,
    trait_ident: FnMutThunk,
    thunk_template: THUNK_TEMPLATE_MUT,
    bare_toggle: cfg(all()),
    bare_receiver: &Self,
    fn_trait_doc: "[`FnMut`]",
    ty_name_doc: "[`BareFnMutAny`]",
    with_cc_doc: "[`with_cc`](BareFnMutAny::with_cc)",
    with_cc_in_doc: "[`with_cc_in`](BareFnMutAny::with_cc_in)",
    try_with_cc_in_doc: "[`try_with_cc_in`](BareFnMutAny::try_with_cc_in)",
    non_sync_alias_doc:  "[`BareFnMut`]",
    sync_alias_doc: "[`BareFnMutSync`]",
    sync_alias_bound_doc: "[`S = dyn Send + 'a`](Send)",
    safety_doc: "- The mutable borrow induced by a previous call is still active (e.g. through recursion)
  or concurrent (has no happens-before relationship) with the current one.\n
- The closure is not `Send`, if calling from a different thread than the current one."
);
bare_closure_impl!(
    ty_name: BareFnAny,
    erased_ty_name: UntypedBareFn,
    non_sync_alias: BareFn,
    sync_alias: BareFnSync,
    sync_bounds: (Sync),
    sync_alias_bound: dyn Send + Sync + 'a,
    trait_ident: FnThunk,
    thunk_template: THUNK_TEMPLATE,
    bare_toggle: cfg(all()),
    bare_receiver: &Self,
    fn_trait_doc: "[`Fn`]",
    ty_name_doc: "[`BareFnAny`]",
    with_cc_doc: "[`with_cc`](BareFnAny::with_cc)",
    with_cc_in_doc: "[`with_cc_in`](BareFnAny::with_cc_in)",
    try_with_cc_in_doc: "[`try_with_cc_in`](BareFnAny::try_with_cc_in)",
    non_sync_alias_doc:  "[`BareFn`]",
    sync_alias_doc: "[`BareFnSync`]",
    sync_alias_bound_doc: "[`S = dyn Send + Sync + 'a`](Sync)",
    safety_doc: "- The closure is not `Sync`, if calling from a different thread than the current one."
);