From 7d15587e3f3b5d7e992db4b12755caf289c2e31d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Sat, 30 Nov 2024 11:57:12 -0300 Subject: [PATCH] Minor improvement cache avoid clone (#479) Avoid cloning to serialize to JSON; instead, dereference the object, as Serde needs a reference to the object. --- crates/cdk-axum/src/router_handlers.rs | 10 ++++++---- crates/cdk-axum/src/ws/subscribe.rs | 7 +------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/crates/cdk-axum/src/router_handlers.rs b/crates/cdk-axum/src/router_handlers.rs index 29300be8e..4d6e7e01d 100644 --- a/crates/cdk-axum/src/router_handlers.rs +++ b/crates/cdk-axum/src/router_handlers.rs @@ -26,7 +26,9 @@ macro_rules! post_cache_wrapper { state: State, payload: Json<$request_type> ) -> Result, Response> { - let Json(json_extracted_payload) = payload.clone(); + use std::ops::Deref; + + let json_extracted_payload = payload.deref(); let State(mint_state) = state.clone(); let cache_key = serde_json::to_string(&json_extracted_payload).map_err(|err| { into_response(Error::from(err)) @@ -37,11 +39,11 @@ macro_rules! post_cache_wrapper { .expect("Shouldn't panic: response is json-deserializable."))); } - let Json(response) = $handler(state, payload).await?; - mint_state.cache.insert(cache_key, serde_json::to_string(&response) + let response = $handler(state, payload).await?; + mint_state.cache.insert(cache_key, serde_json::to_string(response.deref()) .expect("Shouldn't panic: response is json-serializable.") ).await; - Ok(Json(response)) + Ok(response) } } }; diff --git a/crates/cdk-axum/src/ws/subscribe.rs b/crates/cdk-axum/src/ws/subscribe.rs index 06505eb74..f177ae08d 100644 --- a/crates/cdk-axum/src/ws/subscribe.rs +++ b/crates/cdk-axum/src/ws/subscribe.rs @@ -54,12 +54,7 @@ impl WsHandle for Method { return Err(WsError::InvalidParams); } - let mut subscription = context - .state - .mint - .pubsub_manager - .subscribe(self.0.clone()) - .await; + let mut subscription = context.state.mint.pubsub_manager.subscribe(self.0).await; let publisher = context.publisher.clone(); context.subscriptions.insert( sub_id.clone(),