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
//! Since `StackBox<'_, dyn FnOnce…>` does not auto-implement `FnOnce…`, we
//! need to do it manually.
//!
//! ### Higher-order lifetimes are not _directly_ supported by these types
//!
//! See [`custom_dyn!`][`crate::custom_dyn`] to know what this means, and a way
//! to palliate the issue.
#![allow(nonstandard_style)]

use super::*;

mod T { pub use crate::marker::Sendness::T as Sendness; }

generate!(_9 _8 _7 _6 _5 _4 _3 _2 _1 _0); macro_rules! generate {() => (); (
    $_N:tt $($_K:tt)*
) => (generate! { $($_K)* } ::paste::paste! {
    pub use [<FnOnce$_N>]::[<StackBoxDynFnOnce$_N>];
    mod [<FnOnce$_N>] {
        use super::*;

        pub
        struct [<StackBoxDynFnOnce$_N>] <
                'frame, $(
                [</*Arg*/$_K>], )*
                Ret,
                Sendness : ?Sized + T::Sendness = NoAutoTraits,
            >
        {
            ptr: ptr::NonNull<ty::Erased>,
            vtable: &'frame VTable<$([</*Arg*/$_K>] ,)* Ret>,
            _is_send: ::core::marker::PhantomData<Sendness>,
        }

        struct VTable<$([</*Arg*/$_K>] ,)* Ret> {
            drop_in_place: unsafe fn(ptr: ptr::NonNull<ty::Erased>),
            call_once: unsafe fn(
                ptr::NonNull<ty::Erased> $(,
                [</*Arg*/$_K>] )*
            ) -> Ret,
        }

        impl<$([</*Arg*/$_K>], )* Ret, F> HasVTable<$([</*Arg*/$_K>] ,)* Ret> for F
        where
            Self : Sized + FnOnce($([</*Arg*/$_K>]),*) -> Ret,
        {}
        trait HasVTable<$([</*Arg*/$_K>] ,)* Ret>
        where
            Self : Sized + FnOnce($([</*Arg*/$_K>]),*) -> Ret,
        {
            const VTABLE: VTable<$([</*Arg*/$_K>] ,)* Ret> = VTable {
                drop_in_place: {
                    unsafe
                    fn drop_in_place<Self_> (ptr: ptr::NonNull<ty::Erased>)
                    {
                        ptr::drop_in_place(ptr.cast::<Self_>().as_ptr())
                    }
                    drop_in_place::<Self>
                },
                call_once: {
                    unsafe
                    fn call_once<Self_, $([</*Arg*/$_K>] ,)* Ret> (
                        ptr: ptr::NonNull<ty::Erased> $(,
                        [</*arg*/$_K>]: [</*Arg*/$_K>] )*
                    ) -> Ret
                    where
                        Self_ : FnOnce($([</*Arg*/$_K>]),*) -> Ret,
                    {
                        let f: StackBox<'_, Self_> = {
                            ::core::mem::transmute(ptr)
                        };
                        let f: Self_ = StackBox::into_inner(f);
                        f($([</*arg*/$_K>]),*)
                    }
                    call_once::<Self, $([</*Arg*/$_K>] ,)* Ret>
                },
            };
        }

        impl<'frame, $([</*Arg*/$_K>] ,)* Ret, F : 'frame>
            DynCoerce<StackBox<'frame, F>>
        for
            [<StackBoxDynFnOnce$_N>]<'frame, $([</*Arg*/$_K>] ,)* Ret>
        where
            F : FnOnce($([</*Arg*/$_K>]),*) -> Ret,
        {
            #[inline]
            fn fatten (it: StackBox<'frame, F>)
              -> Self
            {
                [<StackBoxDynFnOnce$_N>] {
                    vtable: &<F as HasVTable<$([</*Arg*/$_K>] ,)* Ret>>::VTABLE,
                    ptr: unsafe { ::core::mem::transmute(it) },
                    _is_send: ::core::marker::PhantomData,
                }
            }
        }

        /// And now with the `Send` bound
        impl<'frame, $([</*Arg*/$_K>] ,)* Ret, F : 'frame>
            DynCoerce<StackBox<'frame, F>>
        for
            [<StackBoxDynFnOnce$_N>]<'frame, $([</*Arg*/$_K>] ,)* Ret, dyn Send>
        where
            F : FnOnce($([</*Arg*/$_K>]),*) -> Ret,
            F : Send,
        {
            #[inline]
            fn fatten (it: StackBox<'frame, F>)
              -> Self
            {
                [<StackBoxDynFnOnce$_N>] {
                    vtable: &<F as HasVTable<$([</*Arg*/$_K>] ,)* Ret>>::VTABLE,
                    ptr: unsafe { ::core::mem::transmute(it) },
                    _is_send: ::core::marker::PhantomData,
                }
            }
        }

        impl<'frame, $([</*Arg*/$_K>] ,)* Ret, Sendness : ?Sized + T::Sendness>
            [<StackBoxDynFnOnce$_N>]<'frame, $([</*Arg*/$_K>] ,)* Ret, Sendness>
        {
            #[inline]
            pub
            fn call (
                self: Self $(,
                [</*arg*/$_K>]: [</*Arg*/$_K>] )*
            ) -> Ret
            {
                unsafe {
                    let Self { ptr, vtable, .. } =
                        *::core::mem::ManuallyDrop::new(self)
                    ;
                    (vtable.call_once)(ptr, $([</*arg*/$_K>]),*)
                }
            }
        }

        impl<'frame, $([</*Arg*/$_K>] ,)* Ret, Sendness : ?Sized + T::Sendness> Drop
            for [<StackBoxDynFnOnce$_N>]<'frame, $([</*Arg*/$_K>] ,)* Ret, Sendness>
        {
            #[inline]
            fn drop (self: &'_ mut Self)
            {
                unsafe {
                    (self.vtable.drop_in_place)(self.ptr)
                }
            }
        }

        unsafe // Safety: no shared API whatsoever
            impl<'frame, $([</*Arg*/$_K>] ,)* Ret, Sendness : ?Sized + T::Sendness>
                Sync
            for
                [<StackBoxDynFnOnce$_N>]<'frame, $([</*Arg*/$_K>] ,)* Ret, Sendness>
            {}

        unsafe // Safety: `Sendness = dyn Send` requires a `Send` bound on `F`:
            impl<'frame, $([</*Arg*/$_K>] ,)* Ret>
                Send
            for
                [<StackBoxDynFnOnce$_N>]<'frame, $([</*Arg*/$_K>] ,)* Ret, dyn Send>
            {}
    }
})} use generate;