rust-macros

How do I use a macro across module files?

巧了我就是萌 提交于 2019-12-17 06:35:54
问题 I have two modules in separate files within the same crate, where the crate has macro_rules enabled. I want to use the macros defined in one module in another module. // macros.rs #[macro_export] // or not? is ineffectual for this, afaik macro_rules! my_macro(...) // something.rs use macros; // use macros::my_macro; <-- unresolved import (for obvious reasons) my_macro!() // <-- how? I currently hit the compiler error " macro undefined: 'my_macro' "... which makes sense; the macro system runs

How do I use a macro across module files?

不想你离开。 提交于 2019-12-17 06:35:26
问题 I have two modules in separate files within the same crate, where the crate has macro_rules enabled. I want to use the macros defined in one module in another module. // macros.rs #[macro_export] // or not? is ineffectual for this, afaik macro_rules! my_macro(...) // something.rs use macros; // use macros::my_macro; <-- unresolved import (for obvious reasons) my_macro!() // <-- how? I currently hit the compiler error " macro undefined: 'my_macro' "... which makes sense; the macro system runs

How can I use a macro to generate enum variants that may or may not contain associated values?

﹥>﹥吖頭↗ 提交于 2019-12-13 17:03:51
问题 I'm trying to make a macro to define some enum variants that represent assembler instructions. Right now, I have the following which works fine. This is pretty useless, but eventually I'm going to add some automatically derived methods so it'll have a purpose later on: macro_rules! define_instructions { ($($inames:tt),+ $(,)*) => { #[derive(Debug)] pub enum Instruction { $($inames),* } }; } define_instructions! { PsqLux, PsqLx, Twi, } fn main() {} The problem is that most (but not all) of the

Is it possible to create a macro that implements Ord by delegating to a struct member?

╄→гoц情女王★ 提交于 2019-12-12 15:50:27
问题 I have a struct: struct Student { first_name: String, last_name: String, } I want to create a Vec<Student> that can be sorted by last_name . I need to implement Ord , PartialOrd and PartialEq : use std::cmp::Ordering; impl Ord for Student { fn cmp(&self, other: &Student) -> Ordering { self.last_name.cmp(&other.last_name) } } impl PartialOrd for Student { fn partial_cmp(&self, other: &Student) -> Option<Ordering> { Some(self.cmp(other)) } } impl PartialEq for Student { fn eq(&self, other:

Is it possible to get the expansion of a single macro instead of the whole file?

天大地大妈咪最大 提交于 2019-12-09 07:21:23
问题 I just found How do I see the expanded macro code that's causing my compile error?. Is it possible to get the expansion of a single macro instead of the whole file? 回答1: The cargo-expand command is really just a thin wrapper around cargo rustc -- -Zunstable-options --pretty=expanded , which is itself a blunt instrument. You can't target a specific macro. However, since version 0.4, you can reduce some noise by specifying an extra path argument to expand only macros used by that module: $

How to report errors in a procedural macro using the quote macro?

丶灬走出姿态 提交于 2019-12-08 15:52:18
问题 I am writing a procedural macro which works fine, but I am having trouble reporting errors in an ergonomic way. Using panic! "works" but is not elegant and does not present the error message to the user nicely. I know that I can report good errors while parsing a TokenStream , but I need to produce errors while traversing the AST after it has been parsed. The macro invocation looks like this: attr_test! { #[bool] FOO } And should output: const FOO: bool = false; This is the macro code: extern

Specifying generic parameter to belong to a small set of types

不问归期 提交于 2019-12-02 06:43:05
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. Peter Hall You could use a marker trait for the types you want to support: trait DataSupported {} impl DataSupported for u64 {} impl DataSupported for u32 {} impl<T> Data<T> where T: DataSupported {} As

print! macro gets executed out of order [duplicate]

北城以北 提交于 2019-12-02 02:52:55
This question already has an answer here: Why does this read input before printing? 2 answers 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 this: Usage instructions and stuff [my command] Command: [my command] Any ideas why would that happen? AFAIK, there's no

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

▼魔方 西西 提交于 2019-12-01 19:35:18
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 this macro is used fn select_item(kind: String) -> impl SomeTrait { match kind { "person" => Person,

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

…衆ロ難τιáo~ 提交于 2019-11-30 11:07:10
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); let field_count = my_struct2.field_count(); // Expecting to get count 1 Is there any API like field