Skip to content

Conversation

lbeschastny
Copy link

@lbeschastny lbeschastny commented Jul 28, 2025

Using proc macros should not require importing std::time::Duration

Motivation

Apart from just being a good practice, current implementation makes it difficult to use cached in some cases.

For example, I use it like this in one on my projects:

#[cfg_attr(
    not(test),
    cached::proc_macro::once(result = true, sync_writes = true, time = 900)
)]

but after updating cached to 0.56.0 I started getting an error

 1  error[E0433]: failed to resolve: use of undeclared type `Duration`                                                                                                                      ▐                                              ▐
    --> src/edp.rs:126:5                                                                                                                                                                    ▐                                              ▐
     |                                                                                                                                                                                      ▐                                              ▐
 126 |     cached::proc_macro::once(result = true, sync_writes = true, time = 900)
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `Duration`

and the only way to fix this without getting an unused import warning is to add something like

#[cfg(not(test))]
use std::time::Duration;

which just creates unnecessary complexity.

Using proc macros should not require importing `std::time::Duration`
Comment on lines 225 to 243
match time_refresh {
Some(time_refresh) => {
if asyncness.is_some() {
quote! { cached::AsyncRedisCache::new(#cache_prefix, Duration::from_secs(#time)).set_refresh(#time_refresh).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
quote! { cached::AsyncRedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).set_refresh(#time_refresh).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
} else {
quote! {
cached::RedisCache::new(#cache_prefix, Duration::from_secs(#time)).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
cached::RedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
}
}
}
None => {
if asyncness.is_some() {
quote! { cached::AsyncRedisCache::new(#cache_prefix, Duration::from_secs(#time)).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
quote! { cached::AsyncRedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
} else {
quote! {
cached::RedisCache::new(#cache_prefix, Duration::from_secs(#time)).build().expect("error constructing RedisCache in #[io_cached] macro")
cached::RedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).build().expect("error constructing RedisCache in #[io_cached] macro")
}
}
}
Copy link
Author

@lbeschastny lbeschastny Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition could be rewritten to look more compact:

match (asyncness.is_some(), time_refresh) {
    (true, Some(time_refresh)) => quote! {
        cached::AsyncRedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).set_refresh(#time_refresh).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro")
    },
    (true, None) => quote! {
        cached::AsyncRedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro")
    },
    (false, Some(time_refresh)) => quote! {
        cached::RedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
    },
    (false, None) => quote! {
        cached::RedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).build().expect("error constructing RedisCache in #[io_cached] macro")
    },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant