Path: blob/main/crates/versioned-export-macros/src/lib.rs
1693 views
//! This crate defines macros to easily define and use functions with a1//! versioned suffix, to facilitate using multiple versions of the same2//! crate that generate assembly.3//!4//! > **⚠️ Warning ⚠️**: this crate is an internal-only crate for the Wasmtime5//! > project and is not intended for general use. APIs are not strictly6//! > reviewed for safety and usage outside of Wasmtime may have bugs. If7//! > you're interested in using this feel free to file an issue on the8//! > Wasmtime repository to start a discussion about doing so, but otherwise9//! > be aware that your usage of this crate is not supported.1011use quote::ToTokens;1213const VERSION: &str = env!("CARGO_PKG_VERSION");1415fn version(value: impl std::fmt::Display) -> String {16format!("{}_{}", value, VERSION.replace('.', "_"))17}1819fn versioned_lit_str(value: impl std::fmt::Display) -> syn::LitStr {20syn::LitStr::new(version(value).as_str(), proc_macro2::Span::call_site())21}2223#[proc_macro_attribute]24pub fn versioned_export(25_attr: proc_macro::TokenStream,26item: proc_macro::TokenStream,27) -> proc_macro::TokenStream {28let mut function = syn::parse_macro_input!(item as syn::ItemFn);2930let export_name = versioned_lit_str(&function.sig.ident);31function32.attrs33.push(syn::parse_quote! { #[unsafe(export_name = #export_name)] });3435function.to_token_stream().into()36}3738#[proc_macro_attribute]39pub fn versioned_link(40_attr: proc_macro::TokenStream,41item: proc_macro::TokenStream,42) -> proc_macro::TokenStream {43let mut function = syn::parse_macro_input!(item as syn::ForeignItemFn);4445let link_name = versioned_lit_str(&function.sig.ident);46function47.attrs48.push(syn::parse_quote! { #[link_name = #link_name] });4950function.to_token_stream().into()51}5253#[proc_macro]54pub fn versioned_stringify_ident(item: proc_macro::TokenStream) -> proc_macro::TokenStream {55let ident = syn::parse_macro_input!(item as syn::Ident);5657versioned_lit_str(ident).to_token_stream().into()58}5960#[proc_macro]61pub fn versioned_suffix(item: proc_macro::TokenStream) -> proc_macro::TokenStream {62if !item.is_empty() {63return syn::Error::new(64proc_macro2::Span::call_site(),65"`versioned_suffix!` accepts no input",66)67.to_compile_error()68.into();69};7071versioned_lit_str("").to_token_stream().into()72}737475