diff --git a/Cargo.toml b/Cargo.toml index cb35f8543..a02b6a5b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,6 @@ [workspace] members = [ "serde", - "serde_derive", "serde_derive_internals", "serde_test", "test_suite", diff --git a/serde_derive/Cargo.toml b/serde_derive/Cargo.toml index eaa99e6e5..7c42a4d3b 100644 --- a/serde_derive/Cargo.toml +++ b/serde_derive/Cargo.toml @@ -17,8 +17,7 @@ default = [] deserialize_in_place = [] [lib] -name = "serde_derive" -proc-macro = true +crate-type = ["cdylib"] [dependencies] proc-macro2 = "1.0" @@ -30,3 +29,8 @@ serde = { version = "1.0", path = "../serde" } [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] + +[workspace] + +[patch.crates-io] +proc-macro2 = { git = "https://github.com/dtolnay/watt" } diff --git a/serde_derive/src/lib.rs b/serde_derive/src/lib.rs index 34d2e10fa..34e5beae6 100644 --- a/serde_derive/src/lib.rs +++ b/serde_derive/src/lib.rs @@ -68,12 +68,11 @@ extern crate quote; #[macro_use] extern crate syn; -extern crate proc_macro; extern crate proc_macro2; mod internals; -use proc_macro::TokenStream; +use proc_macro2::TokenStream; use syn::DeriveInput; #[macro_use] @@ -87,23 +86,27 @@ mod pretend; mod ser; mod try; -#[proc_macro_derive(Serialize, attributes(serde))] -pub fn derive_serialize(input: TokenStream) -> TokenStream { - let mut input = parse_macro_input!(input as DeriveInput); - ser::expand_derive_serialize(&mut input) - .unwrap_or_else(to_compile_errors) - .into() +#[no_mangle] +pub extern "C" fn derive_serialize(input: TokenStream) -> TokenStream { + let mut input: DeriveInput = match syn::parse2(input) { + Ok(input) => input, + Err(err) => return err.to_compile_error(), + }; + + ser::expand_derive_serialize(&mut input).unwrap_or_else(to_compile_errors) } -#[proc_macro_derive(Deserialize, attributes(serde))] -pub fn derive_deserialize(input: TokenStream) -> TokenStream { - let mut input = parse_macro_input!(input as DeriveInput); - de::expand_derive_deserialize(&mut input) - .unwrap_or_else(to_compile_errors) - .into() +#[no_mangle] +pub extern "C" fn derive_deserialize(input: TokenStream) -> TokenStream { + let mut input: DeriveInput = match syn::parse2(input) { + Ok(input) => input, + Err(err) => return err.to_compile_error(), + }; + + de::expand_derive_deserialize(&mut input).unwrap_or_else(to_compile_errors) } -fn to_compile_errors(errors: Vec) -> proc_macro2::TokenStream { +fn to_compile_errors(errors: Vec) -> TokenStream { let compile_errors = errors.iter().map(syn::Error::to_compile_error); quote!(#(#compile_errors)*) } diff --git a/wa-serde-derive/.gitignore b/wa-serde-derive/.gitignore new file mode 100644 index 000000000..19e1bced9 --- /dev/null +++ b/wa-serde-derive/.gitignore @@ -0,0 +1 @@ +*.wasm diff --git a/wa-serde-derive/Cargo.toml b/wa-serde-derive/Cargo.toml new file mode 100644 index 000000000..66377ce70 --- /dev/null +++ b/wa-serde-derive/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "wa-serde-derive" +version = "0.1.137" +authors = ["David Tolnay "] +license = "MIT OR Apache-2.0" +description = "serde_derive compiled to wasm" +repository = "https://github.com/dtolnay/watt" +include = ["src"] + +[lib] +proc-macro = true + +[dependencies] +watt = "0.4" + +[workspace] diff --git a/wa-serde-derive/src/lib.rs b/wa-serde-derive/src/lib.rs new file mode 100644 index 000000000..f7b0c8106 --- /dev/null +++ b/wa-serde-derive/src/lib.rs @@ -0,0 +1,18 @@ +extern crate proc_macro; +extern crate watt; + +use proc_macro::TokenStream; +use watt::WasmMacro; + +static MACRO: WasmMacro = WasmMacro::new(WASM); +static WASM: &[u8] = include_bytes!("serde_derive.wasm"); + +#[proc_macro_derive(Serialize, attributes(serde))] +pub fn derive_serialize(input: TokenStream) -> TokenStream { + MACRO.proc_macro_derive("derive_serialize", input) +} + +#[proc_macro_derive(Deserialize, attributes(serde))] +pub fn derive_deserialize(input: TokenStream) -> TokenStream { + MACRO.proc_macro_derive("derive_deserialize", input) +}