Skip to content

Commit f3f11ed

Browse files
committed
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.
1 parent bea07ab commit f3f11ed

File tree

1 file changed

+60
-57
lines changed

1 file changed

+60
-57
lines changed

crates/backend/src/codegen.rs

+60-57
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ impl ToTokens for ast::ImportType {
519519
None => "",
520520
Some(comment) => comment,
521521
};
522+
let const_name = format!("__wbg_generated_const_{}", name);
523+
let const_name = Ident::new(&const_name, Span::call_site());
522524
(quote! {
523525
#[allow(bad_style)]
524526
#(#attrs)*
@@ -527,84 +529,85 @@ impl ToTokens for ast::ImportType {
527529
obj: ::wasm_bindgen::JsValue,
528530
}
529531

530-
impl ::wasm_bindgen::describe::WasmDescribe for #name {
531-
fn describe() {
532-
::wasm_bindgen::JsValue::describe();
532+
#[allow(bad_style)]
533+
const #const_name: () = {
534+
use wasm_bindgen::convert::{IntoWasmAbi, FromWasmAbi, Stack};
535+
use wasm_bindgen::convert::{OptionIntoWasmAbi, OptionFromWasmAbi};
536+
use wasm_bindgen::convert::RefFromWasmAbi;
537+
use wasm_bindgen::describe::WasmDescribe;
538+
use wasm_bindgen::{JsValue, JsCast};
539+
use wasm_bindgen::__rt::core::mem::ManuallyDrop;
540+
541+
impl WasmDescribe for #name {
542+
fn describe() {
543+
JsValue::describe();
544+
}
533545
}
534-
}
535546

536-
impl ::wasm_bindgen::convert::IntoWasmAbi for #name {
537-
type Abi = <::wasm_bindgen::JsValue as
538-
::wasm_bindgen::convert::IntoWasmAbi>::Abi;
547+
impl IntoWasmAbi for #name {
548+
type Abi = <JsValue as IntoWasmAbi>::Abi;
539549

540-
fn into_abi(self, extra: &mut ::wasm_bindgen::convert::Stack) -> Self::Abi {
541-
self.obj.into_abi(extra)
550+
fn into_abi(self, extra: &mut Stack) -> Self::Abi {
551+
self.obj.into_abi(extra)
552+
}
542553
}
543-
}
544554

545-
impl ::wasm_bindgen::convert::OptionIntoWasmAbi for #name {
546-
fn none() -> Self::Abi { 0 }
547-
}
555+
impl OptionIntoWasmAbi for #name {
556+
fn none() -> Self::Abi { 0 }
557+
}
548558

549-
impl<'a> ::wasm_bindgen::convert::OptionIntoWasmAbi for &'a #name {
550-
fn none() -> Self::Abi { 0 }
551-
}
559+
impl<'a> OptionIntoWasmAbi for &'a #name {
560+
fn none() -> Self::Abi { 0 }
561+
}
552562

553-
impl ::wasm_bindgen::convert::FromWasmAbi for #name {
554-
type Abi = <::wasm_bindgen::JsValue as
555-
::wasm_bindgen::convert::FromWasmAbi>::Abi;
563+
impl FromWasmAbi for #name {
564+
type Abi = <JsValue as FromWasmAbi>::Abi;
556565

557-
unsafe fn from_abi(
558-
js: Self::Abi,
559-
extra: &mut ::wasm_bindgen::convert::Stack,
560-
) -> Self {
561-
#name {
562-
obj: ::wasm_bindgen::JsValue::from_abi(js, extra),
566+
unsafe fn from_abi(js: Self::Abi, extra: &mut Stack) -> Self {
567+
#name {
568+
obj: JsValue::from_abi(js, extra),
569+
}
563570
}
564571
}
565-
}
566572

567-
impl ::wasm_bindgen::convert::OptionFromWasmAbi for #name {
568-
fn is_none(abi: &Self::Abi) -> bool { *abi == 0 }
569-
}
573+
impl OptionFromWasmAbi for #name {
574+
fn is_none(abi: &Self::Abi) -> bool { *abi == 0 }
575+
}
570576

571-
impl<'a> ::wasm_bindgen::convert::IntoWasmAbi for &'a #name {
572-
type Abi = <&'a ::wasm_bindgen::JsValue as
573-
::wasm_bindgen::convert::IntoWasmAbi>::Abi;
577+
impl<'a> IntoWasmAbi for &'a #name {
578+
type Abi = <&'a JsValue as IntoWasmAbi>::Abi;
574579

575-
fn into_abi(self, extra: &mut ::wasm_bindgen::convert::Stack) -> Self::Abi {
576-
(&self.obj).into_abi(extra)
580+
fn into_abi(self, extra: &mut Stack) -> Self::Abi {
581+
(&self.obj).into_abi(extra)
582+
}
577583
}
578-
}
579584

580-
impl ::wasm_bindgen::convert::RefFromWasmAbi for #name {
581-
type Abi = <::wasm_bindgen::JsValue as
582-
::wasm_bindgen::convert::RefFromWasmAbi>::Abi;
583-
type Anchor = ::wasm_bindgen::__rt::core::mem::ManuallyDrop<#name>;
585+
impl RefFromWasmAbi for #name {
586+
type Abi = <JsValue as RefFromWasmAbi>::Abi;
587+
type Anchor = ManuallyDrop<#name>;
584588

585-
unsafe fn ref_from_abi(
586-
js: Self::Abi,
587-
extra: &mut ::wasm_bindgen::convert::Stack,
588-
) -> Self::Anchor {
589-
let tmp = <::wasm_bindgen::JsValue as ::wasm_bindgen::convert::RefFromWasmAbi>
590-
::ref_from_abi(js, extra);
591-
::wasm_bindgen::__rt::core::mem::ManuallyDrop::new(#name {
592-
obj: ::wasm_bindgen::__rt::core::mem::ManuallyDrop::into_inner(tmp),
593-
})
589+
unsafe fn ref_from_abi(js: Self::Abi, extra: &mut Stack) -> Self::Anchor {
590+
let tmp = <JsValue as RefFromWasmAbi>::ref_from_abi(js, extra);
591+
ManuallyDrop::new(#name {
592+
obj: ManuallyDrop::into_inner(tmp),
593+
})
594+
}
594595
}
595-
}
596596

597-
impl From<::wasm_bindgen::JsValue> for #name {
598-
fn from(obj: ::wasm_bindgen::JsValue) -> #name {
599-
#name { obj }
597+
impl From<JsValue> for #name {
598+
fn from(obj: JsValue) -> #name {
599+
#name { obj }
600+
}
600601
}
601-
}
602602

603-
impl From<#name> for ::wasm_bindgen::JsValue {
604-
fn from(obj: #name) -> ::wasm_bindgen::JsValue {
605-
obj.obj
603+
impl From<#name> for JsValue {
604+
fn from(obj: #name) -> JsValue {
605+
obj.obj
606+
}
606607
}
607-
}
608+
609+
()
610+
};
608611
}).to_tokens(tokens);
609612
}
610613
}

0 commit comments

Comments
 (0)