From ab822e81f8e06fbdc274090fc0a3fb31ef7f7ed1 Mon Sep 17 00:00:00 2001 From: alyx Date: Wed, 9 Aug 2023 16:59:03 -0400 Subject: pull down user info --- src/cache.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/cache.rs') diff --git a/src/cache.rs b/src/cache.rs index 482a9b8..dfe23e7 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,4 +1,5 @@ use std::{future::Future, time::*, collections::HashMap, hash::Hash}; +use reqwest::StatusCode; #[derive(Debug)] pub struct AsyncCache { func: F, @@ -8,9 +9,9 @@ pub struct AsyncCache { impl AsyncCache where - F: for<'a> FnMut(&'a K) -> Fut, + for<'a> F: FnMut(&'a K) -> Fut + 'a, K: Hash + PartialEq + Eq + Clone, - Fut: Future> + Fut: Future> { pub fn new(interval: Duration, func: F) -> Self { Self{ @@ -19,7 +20,7 @@ where } } - pub async fn get(&mut self, key: &K) -> Result<&V, &'static str> { + pub async fn get(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { if self.is_stale(&key) { self.renew(&key).await } else { @@ -27,7 +28,7 @@ where } } - pub async fn renew(&mut self, key: &K) -> Result<&V, &'static str> { + pub async fn renew(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { let val = (self.func)(&key).await?; self.cache.insert(key.clone(), (Instant::now(), val)); Ok(&self.cache.get(key).unwrap().1) @@ -51,12 +52,12 @@ where impl AsyncCache where - F: for<'a> FnMut(&'a K) -> Fut, + for<'a> F: FnMut(&'a K) -> Fut + 'a, K: Hash + PartialEq + Eq + Clone, V: Clone, - Fut: Future> + Fut: Future> { - pub async fn get_owned(&mut self, key: &K) -> Result { + pub async fn get_owned(&mut self, key: &K) -> Result { self.get(key).await.cloned() } } -- cgit v1.2.3-54-g00ecf