From 339beb404d145fbc946cf38217d72fe498fe5827 Mon Sep 17 00:00:00 2001 From: ilbertt Date: Fri, 29 Mar 2024 16:29:53 +0100 Subject: [PATCH] chore: cargo clippy fixes --- src/canister-utils/src/lib.rs | 2 +- src/gateway-state/src/lib.rs | 8 +++++++- .../src/canister_poller.rs | 18 +++++++----------- src/ic-websocket-gateway/src/client_session.rs | 12 ++++++------ .../src/client_session_handler.rs | 2 +- src/ic-websocket-gateway/src/manager.rs | 4 ++-- src/ic-websocket-gateway/src/ws_listener.rs | 6 +----- tests/src/test_canister_rs/src/canister.rs | 12 +++++------- tests/src/test_canister_rs/src/lib.rs | 3 ++- 9 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/canister-utils/src/lib.rs b/src/canister-utils/src/lib.rs index 39293d0..34862cb 100644 --- a/src/canister-utils/src/lib.rs +++ b/src/canister-utils/src/lib.rs @@ -217,5 +217,5 @@ pub async fn ws_get_messages( .await .expect("Failed to read HTTP response"); - Decode!(&res, CanisterOutputCertifiedMessages).map_err(|e| IcError::Candid(e)) + Decode!(&res, CanisterOutputCertifiedMessages).map_err(IcError::Candid) } diff --git a/src/gateway-state/src/lib.rs b/src/gateway-state/src/lib.rs index 80077ae..4d25d1b 100644 --- a/src/gateway-state/src/lib.rs +++ b/src/gateway-state/src/lib.rs @@ -11,6 +11,12 @@ pub struct GatewayState { inner: Arc, } +impl Default for GatewayState { + fn default() -> Self { + Self::new() + } +} + impl GatewayState { pub fn new() -> Self { Self { @@ -199,7 +205,7 @@ impl GatewayState { /// /// This function shall be called only if it is guaranteed that the canister entry exists in the gateway state. pub fn remove_failed_canister(&self, canister_id: CanisterPrincipal) { - if let None = self.inner.data.remove(&canister_id) { + if self.inner.data.remove(&canister_id).is_none() { unreachable!("failed canister not found in gateway state"); } } diff --git a/src/ic-websocket-gateway/src/canister_poller.rs b/src/ic-websocket-gateway/src/canister_poller.rs index d095eba..4fd2f01 100644 --- a/src/ic-websocket-gateway/src/canister_poller.rs +++ b/src/ic-websocket-gateway/src/canister_poller.rs @@ -113,15 +113,11 @@ impl CanisterPoller { PollingStatus::MessagesPolled(certified_canister_output) => { let relay_messages_span = span!(parent: &Span::current(), Level::TRACE, "Relay Canister Messages"); - let end_of_queue_reached = { - match certified_canister_output.is_end_of_queue { - Some(is_end_of_queue_reached) => is_end_of_queue_reached, - // if 'is_end_of_queue' is None, the CDK version is < 0.3.1 and does not have such a field - // in this case, assume that the queue is fully drained and therefore will be polled again - // after waiting for 'polling_interval_ms' - None => true, - } - }; + // if 'is_end_of_queue' is None, the CDK version is < 0.3.1 and does not have such a field + // in this case, assume that the queue is fully drained and therefore will be polled again + // after waiting for 'polling_interval_ms' + let end_of_queue_reached = + certified_canister_output.is_end_of_queue.unwrap_or(true); self.update_nonce(&certified_canister_output)?; // relaying of messages cannot be done in a separate task for each polling iteration // as they might interleave and break the correct ordering of messages @@ -233,7 +229,7 @@ impl CanisterPoller { trace!("Start relaying message",); (canister_to_client_message, Span::current()) }); - relay_message(canister_message, &client_channel_tx) + relay_message(canister_message, client_channel_tx) .instrument(canister_message_span) .await; relayed_messages_count += 1; @@ -340,7 +336,7 @@ async fn relay_message( } } -pub fn get_nonce_from_message(key: &String) -> Result { +pub fn get_nonce_from_message(key: &str) -> Result { if let Some(message_nonce_str) = key.split('_').last() { let message_nonce = message_nonce_str .parse() diff --git a/src/ic-websocket-gateway/src/client_session.rs b/src/ic-websocket-gateway/src/client_session.rs index ce3f569..833b217 100644 --- a/src/ic-websocket-gateway/src/client_session.rs +++ b/src/ic-websocket-gateway/src/client_session.rs @@ -284,8 +284,8 @@ impl ClientSession { // replace the field with the canister_id received in the first envelope // this shall not be updated anymore // if canister_id is already set in the struct, we return an error as inspect_ic_ws_open_message shall only be called once - if !self.canister_id.replace(canister_id.clone()).is_none() - || !self.client_key.replace(client_key.clone()).is_none() + if self.canister_id.replace(canister_id).is_some() + || self.client_key.replace(client_key.clone()).is_some() { // if the canister_id or client_key field was already set, // it means that the client sent the WS open message twice, @@ -303,10 +303,10 @@ impl ClientSession { // in case of other errors, we report them and terminate the connection handler task Err(e) => { self.close_ws_session().await?; - return Err(IcWsError::IcWsProtocol(format!( + Err(IcWsError::IcWsProtocol(format!( "IC WS setup failed. Error: {:?}", e - ))); + ))) }, } } @@ -361,7 +361,7 @@ impl ClientSession { let canister_id = self.canister_id.expect("must be set"); // relay the envelope to the IC - self.relay_envelope_to_canister(serialized_envelope, canister_id.clone()) + self.relay_envelope_to_canister(serialized_envelope, canister_id) .await .map_err(|e| IcWsError::IcWsProtocol(e.to_string()))?; @@ -385,7 +385,7 @@ impl ClientSession { self.agent .update_signed(canister_id, serialized_envelope) .await?; - return Ok(()); + Ok(()) } async fn handle_open_transition( diff --git a/src/ic-websocket-gateway/src/client_session_handler.rs b/src/ic-websocket-gateway/src/client_session_handler.rs index 406e898..7a35d63 100644 --- a/src/ic-websocket-gateway/src/client_session_handler.rs +++ b/src/ic-websocket-gateway/src/client_session_handler.rs @@ -300,7 +300,7 @@ impl ClientSessionHandler { // call ws_close so that the client is removed from the canister if let Err(e) = ws_close( &self.agent, - &canister_id, + canister_id, CanisterWsCloseArguments { client_key }, ) .await diff --git a/src/ic-websocket-gateway/src/manager.rs b/src/ic-websocket-gateway/src/manager.rs index 1e7f9cc..6d21ba5 100644 --- a/src/ic-websocket-gateway/src/manager.rs +++ b/src/ic-websocket-gateway/src/manager.rs @@ -30,11 +30,11 @@ impl Manager { // creates a concurrent hashmap with capacity of 32 divided in shards so that each entry can be accessed concurrently without locking the whole state let state: GatewayState = GatewayState::new(); - return Self { + Self { agent, address: gateway_address, state, - }; + } } pub fn get_agent_principal(&self) -> Principal { diff --git a/src/ic-websocket-gateway/src/ws_listener.rs b/src/ic-websocket-gateway/src/ws_listener.rs index a00e12b..2b3c1db 100644 --- a/src/ic-websocket-gateway/src/ws_listener.rs +++ b/src/ic-websocket-gateway/src/ws_listener.rs @@ -218,11 +218,7 @@ impl WsListener { let polling_interval_ms = self.polling_interval_ms; // spawn a session handler task for each incoming client connection - let start = self - .clients_connection_time - .get(&client_id) - .unwrap() - .clone(); + let start = *self.clients_connection_time.get(&client_id).unwrap(); tokio::spawn( async move { diff --git a/tests/src/test_canister_rs/src/canister.rs b/tests/src/test_canister_rs/src/canister.rs index bd59b3a..961e40f 100644 --- a/tests/src/test_canister_rs/src/canister.rs +++ b/tests/src/test_canister_rs/src/canister.rs @@ -16,16 +16,14 @@ pub struct AppMessage { impl AppMessage { fn candid_serialize(&self) -> Vec { - encode_one(&self).unwrap() + encode_one(self).unwrap() } } pub fn on_open(args: OnOpenCallbackArgs) { // add client to the list of connected clients CLIENTS_CONNECTED.with(|clients_connected| { - clients_connected - .borrow_mut() - .insert(args.client_principal.clone()); + clients_connected.borrow_mut().insert(args.client_principal); print(format!( "[on_open] # clients connected: {}", @@ -46,16 +44,16 @@ pub fn on_message(args: OnMessageCallbackArgs) { text: app_msg.clone().text + " ping", timestamp: time(), }; - print(format!("[on_message] Received message")); + print("[on_message] Received message"); send_app_message(args.client_principal, new_msg) } fn send_app_message(client_key: ClientPrincipal, msg: AppMessage) { - print(format!("Sending message")); + print("Sending message"); if let Err(e) = send(client_key, msg.candid_serialize()) { print(format!("Could not send message: {}", e)); } - print(format!("Message sent")); + print("Message sent"); } pub fn on_close(args: OnCloseCallbackArgs) { diff --git a/tests/src/test_canister_rs/src/lib.rs b/tests/src/test_canister_rs/src/lib.rs index 1afbc5d..41af759 100644 --- a/tests/src/test_canister_rs/src/lib.rs +++ b/tests/src/test_canister_rs/src/lib.rs @@ -60,7 +60,8 @@ fn ws_get_messages(args: CanisterWsGetMessagesArguments) -> CanisterWsGetMessage ic_websocket_cdk::ws_get_messages(args) } -//// Debug/tests methods +// Debug/tests methods + // send a message to the client, usually called by the canister itself #[update] fn send(client_key: ClientPrincipal, msg_bytes: Vec) -> CanisterSendResult {