Skip to content

Commit 8e8d857

Browse files
authored
Merge pull request #247 from lbeschastny/fix/duration-imports
Qualify `std::time::Duration` in proc macros
2 parents 403774c + e1cb429 commit 8e8d857

File tree

6 files changed

+9
-10
lines changed

6 files changed

+9
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44
## Added
55
## Changed
6+
- Using proc macros no longer require importing `std::time::Duration`
67
## Removed
78

89
## [0.56.0 / [cached_proc_macro[0.25.0]]]

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ fn keyed(a: &str, b: &str) -> usize {
8686

8787
```rust
8888
use cached::proc_macro::once;
89-
use std::time::Duration;
9089

9190
/// Only cache the initial function call.
9291
/// Function will be re-executed after the cache

cached_proc_macro/src/cached.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
125125
}
126126
(false, None, Some(time), None, None, time_refresh) => {
127127
let cache_ty = quote! {cached::TimedCache<#cache_key_ty, #cache_value_ty>};
128-
let cache_create = quote! {cached::TimedCache::with_lifespan_and_refresh(Duration::from_secs(#time), #time_refresh)};
128+
let cache_create = quote! {cached::TimedCache::with_lifespan_and_refresh(std::time::Duration::from_secs(#time), #time_refresh)};
129129
(cache_ty, cache_create)
130130
}
131131
(false, Some(size), Some(time), None, None, time_refresh) => {
132132
let cache_ty = quote! {cached::TimedSizedCache<#cache_key_ty, #cache_value_ty>};
133-
let cache_create = quote! {cached::TimedSizedCache::with_size_and_lifespan_and_refresh(#size, Duration::from_secs(#time), #time_refresh)};
133+
let cache_create = quote! {cached::TimedSizedCache::with_size_and_lifespan_and_refresh(#size, std::time::Duration::from_secs(#time), #time_refresh)};
134134
(cache_ty, cache_create)
135135
}
136136
(false, None, None, None, None, _) => {

cached_proc_macro/src/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub(super) fn gen_return_cache_block(
201201
if let Some(time) = &time {
202202
quote! {
203203
let (created_sec, result) = result;
204-
if now.duration_since(*created_sec) < Duration::from_secs(#time) {
204+
if now.duration_since(*created_sec) < std::time::Duration::from_secs(#time) {
205205
#return_cache_block
206206
}
207207
}

cached_proc_macro/src/io_cached.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,19 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
225225
match time_refresh {
226226
Some(time_refresh) => {
227227
if asyncness.is_some() {
228-
quote! { cached::AsyncRedisCache::new(#cache_prefix, Duration::from_secs(#time)).set_refresh(#time_refresh).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
228+
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") }
229229
} else {
230230
quote! {
231-
cached::RedisCache::new(#cache_prefix, Duration::from_secs(#time)).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
231+
cached::RedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).set_refresh(#time_refresh).build().expect("error constructing RedisCache in #[io_cached] macro")
232232
}
233233
}
234234
}
235235
None => {
236236
if asyncness.is_some() {
237-
quote! { cached::AsyncRedisCache::new(#cache_prefix, Duration::from_secs(#time)).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
237+
quote! { cached::AsyncRedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).build().await.expect("error constructing AsyncRedisCache in #[io_cached] macro") }
238238
} else {
239239
quote! {
240-
cached::RedisCache::new(#cache_prefix, Duration::from_secs(#time)).build().expect("error constructing RedisCache in #[io_cached] macro")
240+
cached::RedisCache::new(#cache_prefix, std::time::Duration::from_secs(#time)).build().expect("error constructing RedisCache in #[io_cached] macro")
241241
}
242242
}
243243
}
@@ -297,7 +297,7 @@ pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
297297
None => create,
298298
Some(time) => {
299299
quote! {
300-
(#create).set_lifespan(Duration::from_secs(#time))
300+
(#create).set_lifespan(std::time::Duration::from_secs(#time))
301301
}
302302
}
303303
};

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ fn keyed(a: &str, b: &str) -> usize {
8787
8888
```rust,no_run
8989
use cached::proc_macro::once;
90-
use std::time::Duration;
9190
9291
/// Only cache the initial function call.
9392
/// Function will be re-executed after the cache

0 commit comments

Comments
 (0)