]> git.lizzy.rs Git - rust.git/blob - crates/proc_macro_srv/src/abis/abi_1_55/proc_macro/bridge/closure.rs
Merge #11184
[rust.git] / crates / proc_macro_srv / src / abis / abi_1_55 / proc_macro / bridge / closure.rs
1 //! lib-proc-macro Closure type (equivalent to `&mut dyn FnMut(A) -> R`) that's `repr(C)`.
2 //!
3 //! Copy from <https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/closure.rs>
4 //! augmented with removing unstable features
5
6 #[repr(C)]
7 pub struct Closure<'a, A, R> {
8     call: unsafe extern "C" fn(&mut Env, A) -> R,
9     env: &'a mut Env,
10 }
11
12 struct Env;
13
14 // impl<'a, A, R> !Sync for Closure<'a, A, R> {}
15 // impl<'a, A, R> !Send for Closure<'a, A, R> {}
16
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)
21         }
22         Closure { call: call::<A, R, F>, env: unsafe { &mut *(f as *mut _ as *mut Env) } }
23     }
24 }
25
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) }
29     }
30 }