rust-macros

Why do proc-macros have to be defined in proc-macro crate?

点点圈 提交于 2020-01-30 05:01:19
问题 I was trying to create a derive macro for my trait, to simplify some stuff. I've encountered some problems: the `#[proc_macro_derive]` attribute is only usable with crates of the `proc-macro` crate type and, after the small fix proc-macro=true : proc-macro` crate types cannot export any items other than functions tagged with `#[proc_macro_derive]` currently functions tagged with `#[proc_macro_derive]` must currently reside in the root of the crate` What is the reason for this behaviour? 回答1:

How do I create a proc_macro_attribute?

坚强是说给别人听的谎言 提交于 2020-01-11 07:13:21
问题 Now that proc_macros have been stabilized, how does one create such a thing? From what I've seen, there's the option of putting a #[proc_macro_attribute] annotation on a fn whatsitsname(attrs: TokenStream, code: TokenStream) -> TokenStream , but how can I register it? How can I add custom attributes? 回答1: The Rust compiler has a fairly complete test suite. When looking for examples of newly-introduced features, I frequently start there: $ rg -c proc_macro_attribute src/test/run-pass-fulldeps

How do I create a proc_macro_attribute?

南笙酒味 提交于 2020-01-11 07:13:07
问题 Now that proc_macros have been stabilized, how does one create such a thing? From what I've seen, there's the option of putting a #[proc_macro_attribute] annotation on a fn whatsitsname(attrs: TokenStream, code: TokenStream) -> TokenStream , but how can I register it? How can I add custom attributes? 回答1: The Rust compiler has a fairly complete test suite. When looking for examples of newly-introduced features, I frequently start there: $ rg -c proc_macro_attribute src/test/run-pass-fulldeps

How do I apply a macro attribute to a function defined in a separate module?

半世苍凉 提交于 2019-12-31 05:09:13
问题 I'm interested in using wasm-bindgen via rust-webpack-template to compile Rust code to WebAssembly. However, I'd like to avoid directly wrapping my code with the #[wasm_bindgen] attribute macro directly so that I can separate out the function logic from the generated WebAssembly interface to better organize my project. Instead, I would prefer to have binding generation be in a separate file, for example: mod my_code; use my_code::my_function; #[wasm_bindgen] my_function; // I want to do

Specifying generic parameter to belong to a small set of types

走远了吗. 提交于 2019-12-31 04:05:22
问题 Is it possible with to constrain a generic parameter to be one of the select few types without figuring out what traits precisely define those type? e.g. impl<T> Data<T> where T == u32 || T == u64 Sometimes it's tedious to figure out what all traits to add to where to get the types you want, and sometimes one wouldn't want to allow a type even when it makes syntactic sense because of semantics. 回答1: You could use a marker trait for the types you want to support: trait DataSupported {} impl

How to programmatically get the number of fields of a struct?

天涯浪子 提交于 2019-12-30 03:44:48
问题 I have a custom struct like the following: struct MyStruct { first_field: i32, second_field: String, third_field: u16, } Is it possible to get the number of struct fields programmatically (like, for example, via a method call field_count() ): let my_struct = MyStruct::new(10, "second_field", 4); let field_count = my_struct.field_count(); // Expecting to get 3 For this struct: struct MyStruct2 { first_field: i32, } ... the following call should return 1 : let my_struct_2 = MyStruct2::new(7);

How do I evaluate expressions in Rust's macro system?

拈花ヽ惹草 提交于 2019-12-23 12:56:33
问题 I'm trying to learn the Rust macro system by writing a simple macro that generates a struct based on some unsigned integer type ( u8 , u16 , u32 , u64 ). I want something like this: bitmessage! { struct Header(u16); version: 8, 5; // the first number is the length, second is value data: 8, 5; } To be more specific, I'm looking for some way to store certain information in an unsigned integer type with various offsets. One use case is to read some bytes and construct some kind of "message": [

print! macro gets executed out of order [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-20 03:07:06
问题 This question already has answers here : Why does this read input before printing? (3 answers) Closed last year . A portion of my code looks like this: print_usage_instructions(); print!("Command: "); let stdin = io::stdin(); let mut line = String::new(); stdin.lock().read_line(&mut line).expect("Couldn't process the command."); println!("{}", line); The behaviour I expect here is something like this: Usage instructions and stuff Command: [my command] [my command] However, what happens is

Is it possible to store state within Rust's procedural macros?

喜夏-厌秋 提交于 2019-12-20 01:09:03
问题 Is it possible to build a macro that doesn't output anything but instead stores state to build up a list and then a second macro that will then actually use that data? For example: trait SomeTrait {} #[derive(mark)] struct Person {} impl SomeTrait for Person {} #[derive(mark)] struct Item {} impl SomeTrait for Item {} #[derive(mark)] struct Object {} impl SomeTrait for Object {} create_mapper! // this then outputs the below function //assuming for the fact that data is loaded correctly before

What does the tt metavariable type mean in Rust macros?

蹲街弑〆低调 提交于 2019-12-18 04:33:21
问题 I'm reading a book about Rust, and start playing with Rust macros. All metavariable types are explained there and have examples, except the last one – tt . According to the book, it is a “a single token tree”. I'm curious, what is it and what is it used for? Can you please provide an example? 回答1: That's a notion introduced to ensure that whatever is in a macro invocation correctly matches () , [] and {} pairs. tt will match any single token or any pair of parenthesis/brackets/braces with