-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use with conditional compilation (cfg-macro)? #56
Comments
After playing around with it a bit more, I found a solution, but it is very verbose: #[cfg_attr(feature = "async",
duplicate_item(
fn_name async;
[my_function] [];
[my_function_async] [async];
)
)]
#[cfg_attr(not(feature = "async"),
duplicate_item(
fn_name async;
[my_function] [];
)
)]
async fn fn_name() {
todo!()
} Is there any better way? |
I tried your first example, that you said didn't work, and it seems to work for me when using one my feautures (pretty_errors): With the feature disabled the expansion resulted in: #[cfg(all())]
fn my_function() {
::core::panicking::panic("not yet implemented")
} With the feature enabled result was: #[cfg(all())]
fn my_function() {
::core::panicking::panic("not yet implemented")
}
#[cfg(feature = "pretty_errors")]
async fn my_function_async() {
::core::panicking::panic("not yet implemented")
} However, I then tried to make another feature called "async", and this didn't work. I also tried a few more names, and they didn't work either. For some reason, only two of my feature names would work. This is extremely weird but also not a problem with I don't have time to look into this any more today, but if you can replicate what I've done perhaps this is a bug in the compiler and we should open an issue in rust-lang/rust. |
The following is my example from the OP with your feature (pretty_errors). Did I understand correctly that that worked for you, because it does not work for me 😅 #[duplicate_item(
fn_name cfg_filter async;
[my_function] [all()] [];
[my_function_async] [feature="pretty_errors"] [async];
)]
#[cfg(cfg_filter)]
async fn fn_name() {
todo!()
}
fn shit() {
my_function()
} It does not expand to anything 🤔 and I get the following error |
Trying again, I got the following to work (regardless of feature name, so lets ignore that part of my last comment): #[duplicate::duplicate_item(
fn_name cfg_filter async;
[my_function] [cfg(all())] [];
[my_function_async] [cfg(feature="async")] [async];
)]
#[cfg_filter]
async fn fn_name() {
todo!()
}
fn shit() {
my_function()
} The key was moving Having to do this seems to me as a compiler error. If you look at the rust reference, their example shows that attributes should be expanded in descending order, which would imply |
@DerZade this issue seems to be known (rust-lang/rust#83331) but will probably not be fixed in the immediate future, so I would stick to moving the |
Thank you very much! Moving the |
I use the
duplicate
macro to generate a sync and a async version of a function. I would like to include the async version only if the featureasync
is enabled. Is there any way to achieve that?The following code, does not work. I guess this is because the
cfg
macro has a "higher" priority as theduplicate_item
macro and therefore it just says "code is inactive due to #[cfg] directives: cfg_filter is disabled"Thanks a lot in advance!
The text was updated successfully, but these errors were encountered: