Repository: 06chaynes/http-cache (language: rust)

Issue:
`CacheManger` not implemented for `MokaManager`
![image](https://user-images.githubusercontent.com/6232026/173924015-f037c855-3a6a-480a-81d5-5d8d4019f036.png)

![image](https://user-images.githubusercontent.com/6232026/173924211-011ec4c0-c315-43f1-8a4e-f4a224ea415c.png)

```
error[E0277]: the trait bound `MokaManager: http_cache::CacheManager` is not satisfied
   --> src/client.rs:18:15
    |
18  |         Cache(HttpCache {
    |               ^^^^^^^^^ the trait `http_cache::CacheManager` is not implemented for `MokaManager`
```


Discussion / hints:

Fix patch:
diff --git a/http-cache/src/managers/moka.rs b/http-cache/src/managers/moka.rs
index f844cae..1a3dab0 100644
--- a/http-cache/src/managers/moka.rs
+++ b/http-cache/src/managers/moka.rs
@@ -12,7 +12,7 @@ use url::Url;
 #[derive(Clone)]
 pub struct MokaManager {
     /// The instance of `moka::future::Cache`
-    pub cache: Cache<String, Arc<Vec<u8>>>,
+    pub cache: Arc<Cache<String, Arc<Vec<u8>>>>,
 }
 
 impl fmt::Debug for MokaManager {
@@ -24,7 +24,7 @@ impl fmt::Debug for MokaManager {
 
 impl Default for MokaManager {
     fn default() -> Self {
-        Self { cache: Cache::new(42) }
+        Self::new(Cache::new(42))
     }
 }
 
@@ -39,6 +39,10 @@ fn req_key(method: &str, url: &Url) -> String {
 }
 
 impl MokaManager {
+    /// Create a new manager from a pre-configured Cache
+    pub fn new(cache: Cache<String, Arc<Vec<u8>>>) -> Self {
+        Self { cache: Arc::new(cache) }
+    }
     /// Clears out the entire cache.
     pub async fn clear(&self) -> Result<()> {
         self.cache.invalidate_all();
@@ -48,7 +52,7 @@ impl MokaManager {
 }
 
 #[async_trait::async_trait]
-impl CacheManager for Arc<MokaManager> {
+impl CacheManager for MokaManager {
     async fn get(
         &self,
         method: &str,


Test patch:
diff --git a/http-cache-tests/src/client_reqwest.rs b/http-cache-tests/src/client_reqwest.rs
index e20ddef..4a229ef 100644
--- a/http-cache-tests/src/client_reqwest.rs
+++ b/http-cache-tests/src/client_reqwest.rs
@@ -11,13 +11,13 @@ async fn default_mode() -> anyhow::Result<()> {
     let m = build_mock(CACHEABLE_PUBLIC, TEST_BODY, 200, 1);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
 
     // Construct reqwest client with cache defaults
     let client = ClientBuilder::new(Client::new())
         .with(Cache(HttpCache {
             mode: CacheMode::Default,
-            manager: Arc::clone(&manager),
+            manager: manager.clone(),
             options: None,
         }))
         .build();
@@ -41,13 +41,13 @@ async fn default_mode_with_options() -> anyhow::Result<()> {
     let m = build_mock(CACHEABLE_PUBLIC, TEST_BODY, 200, 1);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
 
     // Construct reqwest client with cache options override
     let client = ClientBuilder::new(Client::new())
         .with(Cache(HttpCache {
             mode: CacheMode::Default,
-            manager: Arc::clone(&manager),
+            manager: manager.clone(),
             options: Some(CacheOptions { shared: false, ..Default::default() }),
         }))
         .build();
@@ -67,13 +67,13 @@ async fn no_cache_mode() -> anyhow::Result<()> {
     let m = build_mock(CACHEABLE_PUBLIC, TEST_BODY, 200, 2);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
 
     // Construct reqwest client with cache defaults
     let client = ClientBuilder::new(Client::new())
         .with(Cache(HttpCache {
             mode: CacheMode::NoCache,
-            manager: Arc::clone(&manager),
+            manager: manager.clone(),
             options: None,
         }))
         .build();
diff --git a/http-cache-tests/src/client_surf.rs b/http-cache-tests/src/client_surf.rs
index 8d92e35..bdd4088 100644
--- a/http-cache-tests/src/client_surf.rs
+++ b/http-cache-tests/src/client_surf.rs
@@ -11,13 +11,13 @@ async fn default_mode() -> surf::Result<()> {
     let m = build_mock(CACHEABLE_PUBLIC, TEST_BODY, 200, 1);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::Default,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -44,13 +44,13 @@ async fn default_mode_with_options() -> surf::Result<()> {
     let m = build_mock(CACHEABLE_PRIVATE, TEST_BODY, 200, 1);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache options override
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::Default,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: Some(CacheOptions { shared: false, ..Default::default() }),
     }));
 
@@ -73,13 +73,13 @@ async fn default_mode_no_cache_response() -> surf::Result<()> {
     let m = build_mock("no-cache", TEST_BODY, 200, 2);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::Default,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -113,13 +113,13 @@ async fn removes_warning() -> surf::Result<()> {
         .expect(1);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::Default,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -147,13 +147,13 @@ async fn no_store_mode() -> surf::Result<()> {
     let m = build_mock(CACHEABLE_PUBLIC, TEST_BODY, 200, 2);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::NoStore,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -177,13 +177,13 @@ async fn no_cache_mode() -> surf::Result<()> {
     let m = build_mock(CACHEABLE_PUBLIC, TEST_BODY, 200, 2);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::NoCache,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -209,13 +209,13 @@ async fn force_cache_mode() -> surf::Result<()> {
     let m = build_mock(CACHEABLE_PUBLIC, TEST_BODY, 200, 1);
     let _mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::ForceCache,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -245,14 +245,14 @@ async fn delete_after_non_get_head_method_request() -> surf::Result<()> {
     let _mock_guard_get = mock_server.register_as_scoped(m_get).await;
     let _mock_guard_post = mock_server.register_as_scoped(m_post).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req_get = Request::new(Method::Get, Url::parse(&url)?);
     let req_post = Request::new(Method::Post, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::Default,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -283,13 +283,13 @@ async fn revalidation_304() -> surf::Result<()> {
         .expect(1);
     let mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::Default,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -321,13 +321,13 @@ async fn revalidation_200() -> surf::Result<()> {
     let m_200 = build_mock(MUST_REVALIDATE, b"updated", 200, 1);
     let mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::Default,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -361,13 +361,13 @@ async fn revalidation_500() -> surf::Result<()> {
         .expect(1);
     let mock_guard = mock_server.register_as_scoped(m).await;
     let url = format!("{}/", &mock_server.uri());
-    let manager = Arc::new(MokaManager::default());
+    let manager = MokaManager::default();
     let req = Request::new(Method::Get, Url::parse(&url)?);
 
     // Construct Surf client with cache defaults
     let client = Client::new().with(Cache(HttpCache {
         mode: CacheMode::Default,
-        manager: Arc::clone(&manager),
+        manager: manager.clone(),
         options: None,
     }));
 
@@ -403,13 +403,13 @@ mod only_if_cached_mode {
         let m = build_mock(CACHEABLE_PUBLIC, TEST_BODY, 200, 0);
         let _mock_guard = mock_server.register_as_scoped(m).await;
         let url = format!("{}/", &mock_server.uri());
-        let manager = Arc::new(MokaManager::default());
+        let manager = MokaManager::default();
         let req = Request::new(Method::Get, Url::parse(&url)?);
 
         // Construct Surf client with cache defaults
         let client = Client::new().with(Cache(HttpCache {
             mode: CacheMode::OnlyIfCached,
-            manager: Arc::clone(&manager),
+            manager: manager.clone(),
             options: None,
         }));
 
@@ -430,13 +430,13 @@ mod only_if_cached_mode {
         let m = build_mock(CACHEABLE_PUBLIC, TEST_BODY, 200, 1);
         let _mock_guard = mock_server.register_as_scoped(m).await;
         let url = format!("{}/", &mock_server.uri());
-        let manager = Arc::new(MokaManager::default());
+        let manager = MokaManager::default();
         let req = Request::new(Method::Get, Url::parse(&url)?);
 
         // Construct Surf client with cache defaults
         let client = Client::new().with(Cache(HttpCache {
             mode: CacheMode::Default,
-            manager: Arc::clone(&manager),
+            manager: manager.clone(),
             options: None,
         }));
 
@@ -452,7 +452,7 @@ mod only_if_cached_mode {
         // Construct Surf client with cache defaults
         let client = Client::new().with(Cache(HttpCache {
             mode: CacheMode::OnlyIfCached,
-            manager: Arc::clone(&manager),
+            manager: manager.clone(),
             options: None,
         }));