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