rust-proc-macros

Is there a way to have a public trait in a proc-macro crate?

北战南征 提交于 2021-02-16 21:20:47
问题 I have a proc-macro crate with a macro that, when expanded, needs to use custom trait implementations for Rust built-in types. I tried to define the trait in the same crate, but Rust tells me that a proc-macro crate can only have public macros (the functions annotated with #[proc_macro] ) and nothing else can be public. So I put the trait in another crate and in the proc-macro crate included it as a dependency. But this means that anyone that wants to use my proc-macro crate has to depend on

Is there a way to have a public trait in a proc-macro crate?

孤街醉人 提交于 2021-02-16 21:20:08
问题 I have a proc-macro crate with a macro that, when expanded, needs to use custom trait implementations for Rust built-in types. I tried to define the trait in the same crate, but Rust tells me that a proc-macro crate can only have public macros (the functions annotated with #[proc_macro] ) and nothing else can be public. So I put the trait in another crate and in the proc-macro crate included it as a dependency. But this means that anyone that wants to use my proc-macro crate has to depend on

Can we get the source code location of the caller in a procedural macro attribute?

可紊 提交于 2020-04-07 05:19:45
问题 I have requirement to get the source location of the caller of every method. I am trying to create a proc_macro_attribute to capture the location and print it. #[proc_macro_attribute] pub fn get_location(attr: TokenStream, item: TokenStream) -> TokenStream { // Get and print file!(), line!() of source // Should print line no. 11 item } #[get_location] fn add(x: u32, y: u32) -> u32 { x + y } fn main() { add(1, 5); // Line No. 11 } 回答1: Ready to use solutions are available (see @timotree 's

Can we get the source code location of the caller in a procedural macro attribute?

拜拜、爱过 提交于 2020-04-07 05:18:18
问题 I have requirement to get the source location of the caller of every method. I am trying to create a proc_macro_attribute to capture the location and print it. #[proc_macro_attribute] pub fn get_location(attr: TokenStream, item: TokenStream) -> TokenStream { // Get and print file!(), line!() of source // Should print line no. 11 item } #[get_location] fn add(x: u32, y: u32) -> u32 { x + y } fn main() { add(1, 5); // Line No. 11 } 回答1: Ready to use solutions are available (see @timotree 's

Convert string into TokenStream

穿精又带淫゛_ 提交于 2020-04-05 06:54:06
问题 Given a string ( str ), how can one convert that into a TokenStream in Rust? I've tried using the quote! macro. let str = "4"; let tokens = quote! { let num = #str; }; // #str is a str not i32 The goal here is to generate tokens for some unknown string of code. let thing = "4"; let tokens = quote! { let thing = #thing }; // i32 or let thing = ""4""; let tokens = quote! { let thing = #thing }; // str 回答1: how can one convert [a string] into a TokenStream Rust has a common trait for converting

How can I store an identifier (`proc_macro::Ident`) as a constant to avoid repeating it?

て烟熏妆下的殇ゞ 提交于 2020-01-24 08:26:09
问题 I am writing a procedural macro and I need to emit a very long identifier multiple times (potentially because of hygiene, for example). I use quote! to create TokenStream s, but I don't want to repeat the long identifier over and over again! For example, I want to generate this code: let very_long_ident_is_very_long_indeed = 3; println!("{}", very_long_ident_is_very_long_indeed); println!("twice: {}", very_long_ident_is_very_long_indeed + very_long_ident_is_very_long_indeed); I know that I

How can I store an identifier (`proc_macro::Ident`) as a constant to avoid repeating it?

不想你离开。 提交于 2020-01-24 08:26:06
问题 I am writing a procedural macro and I need to emit a very long identifier multiple times (potentially because of hygiene, for example). I use quote! to create TokenStream s, but I don't want to repeat the long identifier over and over again! For example, I want to generate this code: let very_long_ident_is_very_long_indeed = 3; println!("{}", very_long_ident_is_very_long_indeed); println!("twice: {}", very_long_ident_is_very_long_indeed + very_long_ident_is_very_long_indeed); I know that I

What is this strange syntax where an enum variant is used as a function?

我的梦境 提交于 2020-01-21 12:53:01
问题 Below is the example given by the mod documentation of syn::parse . enum Item { Struct(ItemStruct), Enum(ItemEnum), } struct ItemStruct { struct_token: Token![struct], ident: Ident, brace_token: token::Brace, fields: Punctuated<Field, Token![,]>, } impl Parse for Item { fn parse(input: ParseStream) -> Result<Self> { let lookahead = input.lookahead1(); if lookahead.peek(Token![struct]) { input.parse().map(Item::Struct) // <-- here } else if lookahead.peek(Token![enum]) { input.parse().map(Item

What is this strange syntax where an enum variant is used as a function?

纵然是瞬间 提交于 2020-01-21 12:52:58
问题 Below is the example given by the mod documentation of syn::parse . enum Item { Struct(ItemStruct), Enum(ItemEnum), } struct ItemStruct { struct_token: Token![struct], ident: Ident, brace_token: token::Brace, fields: Punctuated<Field, Token![,]>, } impl Parse for Item { fn parse(input: ParseStream) -> Result<Self> { let lookahead = input.lookahead1(); if lookahead.peek(Token![struct]) { input.parse().map(Item::Struct) // <-- here } else if lookahead.peek(Token![enum]) { input.parse().map(Item

What is this strange syntax where an enum variant is used as a function?

旧街凉风 提交于 2020-01-21 12:52:42
问题 Below is the example given by the mod documentation of syn::parse . enum Item { Struct(ItemStruct), Enum(ItemEnum), } struct ItemStruct { struct_token: Token![struct], ident: Ident, brace_token: token::Brace, fields: Punctuated<Field, Token![,]>, } impl Parse for Item { fn parse(input: ParseStream) -> Result<Self> { let lookahead = input.lookahead1(); if lookahead.peek(Token![struct]) { input.parse().map(Item::Struct) // <-- here } else if lookahead.peek(Token![enum]) { input.parse().map(Item