158 questions
1
vote
0
answers
118
views
How to emit a custom error on stable rust in a proc macro for a missing dependency
I have a proc macro where I'm generating code like
quote! {
pub mod #name {
use super::some_sub_crate;
// Do other stuff
}
}
This means users of this proc macro must ensure ...
1
vote
1
answer
131
views
How to let rust-analyzer know I do changes in proc macros?
I have a workspace like this:
.
├── main
| └── ... (use those macros)
└── macros
└── ... (define my macros here)
But by default I need to manually restart rust-analyzer every time I do changes ...
11
votes
1
answer
184
views
Derive macro and trait: refer to concrete fn, don't recurse
I'm writing a derive proc-macro that automatically implements certain trait:
trait Foo {
fn go(&self);
}
Now given the following:
#[derive(Foo)]
struct ConcreteFoo;
impl ConcreteFoo {
...
0
votes
1
answer
68
views
How do I interpolate a type as string using quote! within a docstring in an attribute proc macro?
I have this macro definition:
//! Attribute macro to implement a discriminant method for enums with a specific representation type.
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::...
2
votes
1
answer
68
views
How can I make the compile error points to expanded code?
I am going to let the compiling error pointing to the expanded code but now it is only pointing to the outer line deriving the macro.
What is the solution please? Thank you.
proc macro:
fn ...
1
vote
0
answers
116
views
Pass a string concatenation to a macro
I'm trying to generate an enum type at compile-time, based on a json file.
Following guides like https://developerlife.com/2022/03/30/rust-proc-macro/ I made this :
An external crate my_macros since ...
3
votes
1
answer
316
views
How does serde export both a trait and a derive macro with the same name?
I am trying to implement a trait Foo in a foo crate as well an identically named #[derive] proc-macro in an associated crate foo-macros.
The intent is to have something like serde::Deserialize which ...
1
vote
0
answers
168
views
Instrumenting Async functions in rust
I'm trying to create a procedural macro to be applied on functions in my rust code. The macro implements logic to instrument and gather metrics on the applied method. I want to implement a metric ...
0
votes
0
answers
56
views
In Rust's proc_macro_attribute implementation, filter out custom attribute token in a deeply nested tree
In my #[proc_macro_attribute] implementation of the custom attribute, I need to filter out the #[fix] attribute that may exist on an argument of a function. The custom function gets a input: ...
0
votes
1
answer
263
views
Custom `derive` macro without the actual `Trait` to be derived [closed]
Currently, implementing a custom derive macro does not require an actual Trait to exist at all. All it does is to append an arbitrary code block after the annotated item, and apparently to remove the &...
-1
votes
1
answer
76
views
Generate chained like string with enums and struct
I'm experimenting with proc-macro, the goal is that I can chain functions like a struct that contain fields of enums,
Look at the test, the response will be
permission::auth::moderator::execute::...
0
votes
1
answer
189
views
Rust proc macro to do compile time checking if two types are equivelant
Hello I'm creating a proc macro that I can use in my declarative macros that checks if 2 types are the same, and if so, it will select the first block, otherwise it should select the second block. ...
0
votes
1
answer
180
views
How do I make the quote! macro print hex literals?
I'm using quote to auto-generate code for use in an embedded system. Some of the auto-generated code has numbers which would be way easier to read as hex.
Here's an example.
use quote::quote;
fn main(...
4
votes
1
answer
109
views
What is the syn type/struct that represents the nested field offset parameter to offset_of! macro?
If I want to wrap a macro around offset_of!'s nested form, e.g. offset_of!(Foo, x.y.VariantA.0), what should x.y.VariantA.0 get parsed as in syn terms (e.g. which of these https://docs.rs/syn/latest/...
0
votes
1
answer
205
views
How can I handle a string generated from macro within procedural macro?
I use include_str!() to import the string from a file and pass it into a proc_marco, but it does not work. I get an expected string literal error. Here is my code:
macro_rules! ptcl_layer {
() =&...