Skip to main content

wasm_bindgen_downcast_macros/
lib.rs

1extern crate proc_macro;
2
3use quote::quote;
4use syn::DeriveInput;
5
6#[proc_macro_derive(DowncastJS)]
7pub fn downcast_js(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
8    let derive: DeriveInput = syn::parse_macro_input!(tokens);
9
10    let name = &derive.ident;
11    let internal = quote!(wasm_bindgen_downcast::internal);
12
13    quote! {
14        #[#internal::wasm_bindgen]
15        impl #name {
16            #[doc(hidden)]
17            pub fn __wbgd_downcast_token() -> #internal::Symbol {
18                <Self as wasm_bindgen_downcast::DowncastJS>::token().0.clone()
19            }
20        }
21
22        unsafe impl wasm_bindgen_downcast::DowncastJS for #name {
23            fn token() -> &'static #internal::DowncastToken {
24                static TOKEN: #internal::Lazy<#internal::DowncastToken> =
25                    #internal::Lazy::new(|| #internal::DowncastToken::unique());
26
27                &TOKEN
28            }
29        }
30    }
31    .into()
32}