Skip to content

Commit

Permalink
Compile serde_derive to wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jul 2, 2022
1 parent 845b900 commit 1afae18
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[workspace]
members = [
"serde",
"serde_derive",
"serde_derive_internals",
"serde_test",
"test_suite",
Expand Down
8 changes: 6 additions & 2 deletions serde_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ default = []
deserialize_in_place = []

[lib]
name = "serde_derive"
proc-macro = true
crate-type = ["cdylib"]

[dependencies]
proc-macro2 = "1.0"
Expand All @@ -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" }
33 changes: 18 additions & 15 deletions serde_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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<syn::Error>) -> proc_macro2::TokenStream {
fn to_compile_errors(errors: Vec<syn::Error>) -> TokenStream {
let compile_errors = errors.iter().map(syn::Error::to_compile_error);
quote!(#(#compile_errors)*)
}
1 change: 1 addition & 0 deletions wa-serde-derive/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wasm
16 changes: 16 additions & 0 deletions wa-serde-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "wa-serde-derive"
version = "0.1.137"
authors = ["David Tolnay <[email protected]>"]
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]
18 changes: 18 additions & 0 deletions wa-serde-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 1afae18

Please sign in to comment.