cached
cached copied to clipboard
Lifetimes not added to inner fn in proc macro
Given this example function
use cached::proc_macro::cached;
use cached::UnboundCache;
#[cached(
type = "UnboundCache<usize, CreateMessage>",
create = "{ UnboundCache::with_capacity(1) }",
convert = r#"{ cmds.len() }"#
)]
fn generate_help<'a>(cmds: &[Arc<dyn Command>]) -> CreateMessage<'a> {
CreateMessage::default()
}
Expands to
use cached::proc_macro::cached;
use cached::UnboundCache;
static GENERATE_HELP: ::cached::once_cell::sync::Lazy<
std::sync::Mutex<UnboundCache<usize, CreateMessage>>,
> = ::cached::once_cell::sync::Lazy::new(|| {
std::sync::Mutex::new({ UnboundCache::with_capacity(1) })
});
fn generate_help<'a>(cmds: &[Arc<dyn Command>]) -> CreateMessage<'a> {
use cached::Cached;
let key = { cmds.len() };
{
let mut cache = GENERATE_HELP.lock().unwrap();
if let Some(result) = cache.cache_get(&key) {
return result.clone();
}
}
fn inner(cmds: &[Arc<dyn Command>]) -> CreateMessage<'a> {
CreateMessage::default()
}
let result = inner(cmds);
let mut cache = GENERATE_HELP.lock().unwrap();
cache.cache_set(key, result.clone());
result
}
Resulting in
error[E0261]: use of undeclared lifetime name `'a`
--> src/<file>.rs:67:66
|
66 | )]
| - help: consider introducing lifetime `'a` here: `<'a>`
67 | fn generate_help<'a>(cmds: &[Arc<dyn Command>]) -> CreateMessage<'a> {
| ^^ undeclared lifetime
Since the lifetime specifiers aren't copied into the inner
func, we're not able to return types that require specifiers. The same problem exists with the cached!
macro, except that it completely fails to parse the lifetimes instead of translating them wrong.