1 //! lib-proc-macro Closure type (equivalent to `&mut dyn FnMut(A) -> R`) that's `repr(C)`.
3 //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/closure.rs#
4 //! augmented with removing unstable features
7 pub struct Closure<'a, A, R> {
8 call: unsafe extern "C" fn(&mut Env, A) -> R,
14 // impl<'a, A, R> !Sync for Closure<'a, A, R> {}
15 // impl<'a, A, R> !Send for Closure<'a, A, R> {}
17 impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> {
18 fn from(f: &'a mut F) -> Self {
19 unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: &mut Env, arg: A) -> R {
20 (*(env as *mut _ as *mut F))(arg)
22 Closure { call: call::<A, R, F>, env: unsafe { &mut *(f as *mut _ as *mut Env) } }
26 impl<'a, A, R> Closure<'a, A, R> {
27 pub fn call(&mut self, arg: A) -> R {
28 unsafe { (self.call)(self.env, arg) }