Mercurial > lbo > hg > memoize
changeset 31:102b185d2013
Allow any expression resulting in core::time::Duration for TimeToLive
author | Илья Ефимов <inferrna@gmail.com> |
---|---|
date | Fri, 11 Dec 2020 00:52:49 +0800 |
parents | 3be4519e3838 |
children | a2ba1566dea3 |
files | examples/test1.rs src/lib.rs |
diffstat | 2 files changed, 6 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/test1.rs Thu Dec 10 22:48:52 2020 +0800 +++ b/examples/test1.rs Fri Dec 11 00:52:49 2020 +0800 @@ -9,7 +9,7 @@ i: Instant, } -#[memoize(Capacity: 123, TimeToLive: Duration::from_secs(2))] +#[memoize(TimeToLive: Duration::from_secs(2), Capacity: 123)] fn hello(key: String) -> ComplexStruct { println!("hello: {}", key); ComplexStruct { @@ -25,7 +25,7 @@ println!("result: {:?}", hello("ABC".to_string())); println!("result: {:?}", hello("DEF".to_string())); println!("result: {:?}", hello("ABC".to_string())); //Same as first - thread::sleep(Duration::from_millis(2100)); + thread::sleep(core::time::Duration::from_millis(2100)); println!("result: {:?}", hello("EFG".to_string())); println!("result: {:?}", hello("ABC".to_string())); //Refreshed println!("result: {:?}", hello("EFG".to_string())); //Same as first
--- a/src/lib.rs Thu Dec 10 22:48:52 2020 +0800 +++ b/src/lib.rs Fri Dec 11 00:52:49 2020 +0800 @@ -46,8 +46,7 @@ #[cfg(feature = "full")] mod store { use proc_macro::TokenStream; - use syn::{parse as p, ExprCall}; - use std::time::Duration; + use syn::{parse as p, ExprCall, ExprMethodCall, Expr}; use syn::export::ToTokens; use syn::spanned::Spanned; use std::ops::Deref; @@ -56,13 +55,13 @@ #[derive(Default, Clone)] pub(crate) struct CacheOptions { lru_max_entries: Option<usize>, - pub(crate) time_to_live: Option<ExprCall>, + pub(crate) time_to_live: Option<Expr>, } #[derive(Clone)] enum CacheOption { LRUMaxEntries(usize), - TimeToLive(ExprCall), + TimeToLive(Expr), } syn::custom_keyword!(Capacity); @@ -83,7 +82,7 @@ if la.peek(TimeToLive) { let _: TimeToLive = input.parse().unwrap(); let _: Colon = input.parse().unwrap(); - let cap: syn::ExprCall = input.parse().unwrap(); + let cap: syn::Expr = input.parse().unwrap(); return Ok(CacheOption::TimeToLive(cap)); }