Mercurial > lbo > hg > memoize
view examples/full_featured.rs @ 106:bb57757a7727 default tip
Added tag v0.4.1 for changeset 793f2e38a318
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Fri, 27 Oct 2023 20:22:19 +0200 |
parents | 528ce50ec228 |
children |
line wrap: on
line source
use memoize::memoize; use std::thread; use std::time::{Duration, Instant}; #[allow(dead_code)] #[derive(Debug, Clone)] struct ComplexStruct { s: String, b: bool, i: Instant, } #[cfg(feature = "full")] #[memoize(TimeToLive: Duration::from_secs(2), Capacity: 123)] fn hello(key: String) -> ComplexStruct { println!("hello: {}", key); ComplexStruct { s: key, b: false, i: Instant::now(), } } fn main() { #[cfg(feature = "full")] { println!("result: {:?}", hello("ABC".to_string())); println!("result: {:?}", hello("DEF".to_string())); println!("result: {:?}", hello("ABC".to_string())); // Same as first thread::sleep(core::time::Duration::from_millis(2100)); println!("result: {:?}", hello("ABC".to_string())); println!("result: {:?}", hello("DEF".to_string())); println!("result: {:?}", hello("ABC".to_string())); // Same as first memoized_flush_hello(); println!("result: {:?}", hello("EFG".to_string())); println!("result: {:?}", hello("ABC".to_string())); // Refreshed println!("result: {:?}", hello("EFG".to_string())); // Same as first println!("result: {:?}", hello("ABC".to_string())); // Same as refreshed println!("result: {:?}", memoized_original_hello("ABC".to_string())); } }