Closed behnam closed 4 years ago
Woops... that's an oversight!
The Rust API Guidelines calls this C-MACRO-VIS
.
Another point from that checklist that applies here is C-MACRO-ATTR
To fix both, use something like this:
macro_rules! fn_macro {
// Allow `fn foo()`
($(#[$attr:meta])* fn $name:ident($arg:ident: $ty:ty) $fun:block) => {
fn_macro!{
internal $(#[$attr])* [] fn $name($arg: $ty) $fun
}
};
// Allow `pub(...) fn foo()`
($(#[$attr:meta])* pub($($vis:tt)+) fn $name:ident($arg:ident: $ty:ty) $fun:block) => {
fn_macro!{
internal $(#[$attr])* [pub($($vis)+)] fn $name($arg: $ty) $fun
}
};
// Allow `pub fn foo()`
($(#[$attr:meta])* pub fn $name:ident($arg:ident: $ty:ty) $fun:block) => {
fn_macro!{
internal $(#[$attr])* [pub] fn $name($arg: $ty) $fun
}
};
(internal $(#[$attr:meta])* [$($vis:tt)*] fn $name:ident($arg:ident: $ty:ty) $fun:block) => {
$(#[$attr])*
$($vis)* fn $name($arg: $ty) $fun
};
}
fn_macro!{
#[cfg(debug_assertions)]
pub(crate) fn foo(bar: i32) {
println!("{}", bar);
}
}
fn main() {
foo(42);
}
Any chance to have it working out of the box? I also stuck with that issue, cannot use cached. The only solution I see now is to create mirroring function which will be public and will call to internal cached.
But there is another issue which prevents me to do that:
error: no rules expected the token `&`
--> locale_loader/mod.rs:73:29
|
73 | fn translate_cached(&'a self, locale: &str, key: &str) -> String {
I am trying to cache the structure method
The macro doesn't let me assign privacy to the function.