Clean up generated code for imported types

Group all the generated impls in a `const` block so we can use `use` without
clashing with the outside scope.
This commit is contained in:
Alex Crichton
2018-08-04 09:10:23 -07:00
parent bea07abd0f
commit f3f11ed8eb

View File

@ -519,6 +519,8 @@ impl ToTokens for ast::ImportType {
None => "",
Some(comment) => comment,
};
let const_name = format!("__wbg_generated_const_{}", name);
let const_name = Ident::new(&const_name, Span::call_site());
(quote! {
#[allow(bad_style)]
#(#attrs)*
@ -527,84 +529,85 @@ impl ToTokens for ast::ImportType {
obj: ::wasm_bindgen::JsValue,
}
impl ::wasm_bindgen::describe::WasmDescribe for #name {
#[allow(bad_style)]
const #const_name: () = {
use wasm_bindgen::convert::{IntoWasmAbi, FromWasmAbi, Stack};
use wasm_bindgen::convert::{OptionIntoWasmAbi, OptionFromWasmAbi};
use wasm_bindgen::convert::RefFromWasmAbi;
use wasm_bindgen::describe::WasmDescribe;
use wasm_bindgen::{JsValue, JsCast};
use wasm_bindgen::__rt::core::mem::ManuallyDrop;
impl WasmDescribe for #name {
fn describe() {
::wasm_bindgen::JsValue::describe();
JsValue::describe();
}
}
impl ::wasm_bindgen::convert::IntoWasmAbi for #name {
type Abi = <::wasm_bindgen::JsValue as
::wasm_bindgen::convert::IntoWasmAbi>::Abi;
impl IntoWasmAbi for #name {
type Abi = <JsValue as IntoWasmAbi>::Abi;
fn into_abi(self, extra: &mut ::wasm_bindgen::convert::Stack) -> Self::Abi {
fn into_abi(self, extra: &mut Stack) -> Self::Abi {
self.obj.into_abi(extra)
}
}
impl ::wasm_bindgen::convert::OptionIntoWasmAbi for #name {
impl OptionIntoWasmAbi for #name {
fn none() -> Self::Abi { 0 }
}
impl<'a> ::wasm_bindgen::convert::OptionIntoWasmAbi for &'a #name {
impl<'a> OptionIntoWasmAbi for &'a #name {
fn none() -> Self::Abi { 0 }
}
impl ::wasm_bindgen::convert::FromWasmAbi for #name {
type Abi = <::wasm_bindgen::JsValue as
::wasm_bindgen::convert::FromWasmAbi>::Abi;
impl FromWasmAbi for #name {
type Abi = <JsValue as FromWasmAbi>::Abi;
unsafe fn from_abi(
js: Self::Abi,
extra: &mut ::wasm_bindgen::convert::Stack,
) -> Self {
unsafe fn from_abi(js: Self::Abi, extra: &mut Stack) -> Self {
#name {
obj: ::wasm_bindgen::JsValue::from_abi(js, extra),
obj: JsValue::from_abi(js, extra),
}
}
}
impl ::wasm_bindgen::convert::OptionFromWasmAbi for #name {
impl OptionFromWasmAbi for #name {
fn is_none(abi: &Self::Abi) -> bool { *abi == 0 }
}
impl<'a> ::wasm_bindgen::convert::IntoWasmAbi for &'a #name {
type Abi = <&'a ::wasm_bindgen::JsValue as
::wasm_bindgen::convert::IntoWasmAbi>::Abi;
impl<'a> IntoWasmAbi for &'a #name {
type Abi = <&'a JsValue as IntoWasmAbi>::Abi;
fn into_abi(self, extra: &mut ::wasm_bindgen::convert::Stack) -> Self::Abi {
fn into_abi(self, extra: &mut Stack) -> Self::Abi {
(&self.obj).into_abi(extra)
}
}
impl ::wasm_bindgen::convert::RefFromWasmAbi for #name {
type Abi = <::wasm_bindgen::JsValue as
::wasm_bindgen::convert::RefFromWasmAbi>::Abi;
type Anchor = ::wasm_bindgen::__rt::core::mem::ManuallyDrop<#name>;
impl RefFromWasmAbi for #name {
type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = ManuallyDrop<#name>;
unsafe fn ref_from_abi(
js: Self::Abi,
extra: &mut ::wasm_bindgen::convert::Stack,
) -> Self::Anchor {
let tmp = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::RefFromWasmAbi>
::ref_from_abi(js, extra);
::wasm_bindgen::__rt::core::mem::ManuallyDrop::new(#name {
obj: ::wasm_bindgen::__rt::core::mem::ManuallyDrop::into_inner(tmp),
unsafe fn ref_from_abi(js: Self::Abi, extra: &mut Stack) -> Self::Anchor {
let tmp = <JsValue as RefFromWasmAbi>::ref_from_abi(js, extra);
ManuallyDrop::new(#name {
obj: ManuallyDrop::into_inner(tmp),
})
}
}
impl From<::wasm_bindgen::JsValue> for #name {
fn from(obj: ::wasm_bindgen::JsValue) -> #name {
impl From<JsValue> for #name {
fn from(obj: JsValue) -> #name {
#name { obj }
}
}
impl From<#name> for ::wasm_bindgen::JsValue {
fn from(obj: #name) -> ::wasm_bindgen::JsValue {
impl From<#name> for JsValue {
fn from(obj: #name) -> JsValue {
obj.obj
}
}
()
};
}).to_tokens(tokens);
}
}