aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-04-06 16:08:26 +0100
committerGitHub <[email protected]>2020-04-06 16:08:26 +0100
commitf6d688d13070a54b288486900a30680d013c66ca (patch)
tree39262a1e6cb34897723c4c36b7503fc1483c5a9f /crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs
parent2603a9e628d304c8cb8fd08979e2f9c9afeac69e (diff)
parent4f70162f54ceaa4ff68a5ab74e31b5dda7ac9444 (diff)
Merge #3842
3842: Add lib-proc-macro mod in ra_proc_macro_srv r=matklad a=edwin0cheng This PR add a module in ra_proc_macro_srv, which is just copy & paste from rustc lib_proc_macro and remove all unstable features in it. The main idea here is by doing that, we could build the `ra_proc_macro_srv` without nightly compiler and remain ABI compatibility. Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs')
-rw-r--r--crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs
new file mode 100644
index 000000000..b8addff4a
--- /dev/null
+++ b/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs
@@ -0,0 +1,27 @@
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)]
7pub struct Closure<'a, A, R> {
8 call: unsafe extern "C" fn(&mut Env, A) -> R,
9 env: &'a mut Env,
10}
11
12struct Env;
13
14impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> {
15 fn from(f: &'a mut F) -> Self {
16 unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: &mut Env, arg: A) -> R {
17 (*(env as *mut _ as *mut F))(arg)
18 }
19 Closure { call: call::<A, R, F>, env: unsafe { &mut *(f as *mut _ as *mut Env) } }
20 }
21}
22
23impl<'a, A, R> Closure<'a, A, R> {
24 pub fn call(&mut self, arg: A) -> R {
25 unsafe { (self.call)(self.env, arg) }
26 }
27}