jaemk / cached

Rust cache structures and easy function memoization
MIT License
1.54k stars 93 forks source link

Lifetimes not added to inner fn in proc macro #48

Open coadler opened 4 years ago

coadler commented 4 years ago

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.

UnexDev commented 4 months ago

Same issue here. This also causes a borrowed data escapes outside of function with an Arc<T> for some reason.

Will try to fix real quick and open a PR.

UnexDev commented 4 months ago

Same issue here. This also causes a borrowed data escapes outside of function with an Arc<T> for some reason.

Will try to fix real quick and open a PR.

It seems that my issue is not the same. Generics are not copied to the no_cache fn. Will open new issue.