From 708962becdc6044540a07bfc1796654bb8e4cb07 Mon Sep 17 00:00:00 2001 From: xavo95 Date: Fri, 13 Sep 2024 23:41:13 +0200 Subject: [PATCH] Clippy's warnings fixed --- common/src/config_util.rs | 2 +- common/src/time_util.rs | 2 +- game-server/src/logic/components/attribute.rs | 5 +---- game-server/src/logic/ecs/mod.rs | 2 +- game-server/src/logic/ecs/world.rs | 2 +- game-server/src/logic/player/mod.rs | 3 +-- game-server/src/logic/thread_mgr.rs | 3 +-- game-server/src/logic/utils/entity_serializer.rs | 3 +-- gateway-server/kcp/src/kcp.rs | 6 +++--- .../src/handler/client_request_handler.rs | 2 +- gateway-server/src/session/mod.rs | 4 ++-- gateway-server/src/session/util.rs | 3 +-- gateway-server/src/udp_server.rs | 2 +- login-server/src/handler.rs | 13 +++++-------- shorekeeper-http/src/lib.rs | 6 ++++++ shorekeeper-network/src/message.rs | 2 +- shorekeeper-network/src/server.rs | 3 +-- shorekeeper-protocol/build.rs | 2 +- shorekeeper-protocol/src/message.rs | 2 +- shorekeeper-protokey/src/lib.rs | 2 +- 20 files changed, 32 insertions(+), 37 deletions(-) diff --git a/common/src/config_util.rs b/common/src/config_util.rs index a7031a9..ff59adb 100644 --- a/common/src/config_util.rs +++ b/common/src/config_util.rs @@ -4,7 +4,7 @@ pub trait TomlConfig: DeserializeOwned { const DEFAULT_TOML: &str; } -pub fn load_or_create<'a, C>(path: &str) -> C +pub fn load_or_create(path: &str) -> C where C: DeserializeOwned + TomlConfig, { diff --git a/common/src/time_util.rs b/common/src/time_util.rs index 450d598..050c89f 100644 --- a/common/src/time_util.rs +++ b/common/src/time_util.rs @@ -4,7 +4,7 @@ pub fn unix_timestamp() -> u64 { SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() - .as_secs() as u64 + .as_secs() } pub fn unix_timestamp_ms() -> u64 { diff --git a/game-server/src/logic/components/attribute.rs b/game-server/src/logic/components/attribute.rs index 9269f1e..d37c61a 100644 --- a/game-server/src/logic/components/attribute.rs +++ b/game-server/src/logic/components/attribute.rs @@ -26,10 +26,7 @@ macro_rules! impl_from_data { impl Component for Attribute { fn set_pb_data(&self, pb: &mut shorekeeper_protocol::EntityPb) { - pb.living_status = self - .is_alive() - .then_some(LivingStatus::Alive) - .unwrap_or(LivingStatus::Dead) + pb.living_status = (if self.is_alive() { LivingStatus::Alive } else { LivingStatus::Dead }) .into(); pb.component_pbs.push(EntityComponentPb { diff --git a/game-server/src/logic/ecs/mod.rs b/game-server/src/logic/ecs/mod.rs index abd3564..6c51998 100644 --- a/game-server/src/logic/ecs/mod.rs +++ b/game-server/src/logic/ecs/mod.rs @@ -59,6 +59,6 @@ macro_rules! query_components { }), )*) }) - .unwrap_or_else(|| ($( crate::ident_as_none!($comp), )*)) + .unwrap_or_else(|| ($( $crate::ident_as_none!($comp), )*)) }; } diff --git a/game-server/src/logic/ecs/world.rs b/game-server/src/logic/ecs/world.rs index e8dfbe2..28e4291 100644 --- a/game-server/src/logic/ecs/world.rs +++ b/game-server/src/logic/ecs/world.rs @@ -24,7 +24,7 @@ impl World { pub fn create_entity(&mut self) -> EntityBuilder { let entity = self.entity_manager.create(); - EntityBuilder::builder(entity, self.components.entry(entity).or_insert(Vec::new())) + EntityBuilder::builder(entity, self.components.entry(entity).or_default()) } pub fn is_in_world(&self, entity_id: i64) -> bool { diff --git a/game-server/src/logic/player/mod.rs b/game-server/src/logic/player/mod.rs index 323dc7f..671b173 100644 --- a/game-server/src/logic/player/mod.rs +++ b/game-server/src/logic/player/mod.rs @@ -126,8 +126,7 @@ impl Player { .unwrap() .role_id_set .iter() - .map(|id| self.role_list.iter().find(|r| r.role_id == *id)) - .flatten() + .flat_map(|id| self.role_list.iter().find(|r| r.role_id == *id)) .collect() } diff --git a/game-server/src/logic/thread_mgr.rs b/game-server/src/logic/thread_mgr.rs index 9393c93..e91667b 100644 --- a/game-server/src/logic/thread_mgr.rs +++ b/game-server/src/logic/thread_mgr.rs @@ -109,8 +109,7 @@ fn logic_thread_func(receiver: mpsc::Receiver, load: Arc>(); super::systems::tick_systems(&mut world, &mut players); diff --git a/game-server/src/logic/utils/entity_serializer.rs b/game-server/src/logic/utils/entity_serializer.rs index 05d2639..c3d32b8 100644 --- a/game-server/src/logic/utils/entity_serializer.rs +++ b/game-server/src/logic/utils/entity_serializer.rs @@ -11,8 +11,7 @@ pub fn build_scene_add_on_init_data(world: &World) -> PlayerSceneAoiData { let mut aoi_data = PlayerSceneAoiData::default(); for entity in entities { - let mut pb = EntityPb::default(); - pb.id = entity.into(); + let mut pb = EntityPb { id: entity.into(), ..Default::default() }; world .get_entity_components(entity) diff --git a/gateway-server/kcp/src/kcp.rs b/gateway-server/kcp/src/kcp.rs index 7d5ef80..ea88edd 100644 --- a/gateway-server/kcp/src/kcp.rs +++ b/gateway-server/kcp/src/kcp.rs @@ -867,7 +867,7 @@ impl Kcp { } let mut ts_flush = self.ts_flush; - let mut tm_packet = u32::max_value(); + let mut tm_packet = u32::MAX; if timediff(current, ts_flush) >= 10000 || timediff(current, ts_flush) < -10000 { ts_flush = current; @@ -1142,7 +1142,7 @@ impl Kcp { let resent = if self.fastresend > 0 { self.fastresend } else { - u32::max_value() + u32::MAX }; let rtomin = if !self.nodelay { self.rx_rto >> 3 } else { 0 }; @@ -1381,7 +1381,7 @@ impl Kcp { let resent = if self.fastresend > 0 { self.fastresend } else { - u32::max_value() + u32::MAX }; let rtomin = if !self.nodelay { self.rx_rto >> 3 } else { 0 }; diff --git a/gateway-server/src/handler/client_request_handler.rs b/gateway-server/src/handler/client_request_handler.rs index f9a9775..c481643 100644 --- a/gateway-server/src/handler/client_request_handler.rs +++ b/gateway-server/src/handler/client_request_handler.rs @@ -140,7 +140,7 @@ async fn on_login_request( session.player_id = Some(player_id); response.code = ErrorCode::Success.into(); - response.timestamp = time_util::unix_timestamp() as i64; + response.timestamp = time_util::unix_timestamp_ms() as i64; tracing::info!( "login success, user_id: {}, player_id: {}", diff --git a/gateway-server/src/session/mod.rs b/gateway-server/src/session/mod.rs index 37a7d80..c207728 100644 --- a/gateway-server/src/session/mod.rs +++ b/gateway-server/src/session/mod.rs @@ -155,11 +155,11 @@ impl Session { fn next_message(&mut self) -> Option { self.decoder.pop_with(|buf| { - Message::decode(&buf) + Message::decode(buf) .inspect_err(|err| { tracing::error!( "failed to decode a message, err: {err}, buf: {}", - hex::encode(&buf) + hex::encode(buf) ) }) .ok() diff --git a/gateway-server/src/session/util.rs b/gateway-server/src/session/util.rs index 4e49740..effe8ed 100644 --- a/gateway-server/src/session/util.rs +++ b/gateway-server/src/session/util.rs @@ -15,8 +15,7 @@ impl LengthFieldBasedDecoder { pub fn input(&mut self, data: &[u8]) { self.ensure_capacity(data.len()); - - (&mut self.buffer[self.cur_index..self.cur_index + data.len()]).copy_from_slice(data); + self.buffer[self.cur_index..self.cur_index + data.len()].copy_from_slice(data); self.cur_index += data.len(); } diff --git a/gateway-server/src/udp_server.rs b/gateway-server/src/udp_server.rs index 9773ac9..f56ac71 100644 --- a/gateway-server/src/udp_server.rs +++ b/gateway-server/src/udp_server.rs @@ -64,7 +64,7 @@ impl UdpServer { conv_id, addr, self.socket.clone(), - &self.protokey_helper, + self.protokey_helper, self.db.clone(), ); self.session_mgr.add(conv_id, session); diff --git a/login-server/src/handler.rs b/login-server/src/handler.rs index 2e78ff1..27e1259 100644 --- a/login-server/src/handler.rs +++ b/login-server/src/handler.rs @@ -11,13 +11,10 @@ pub async fn handle_login_api_call( tracing::debug!("login requested"); let user_data = parameters.user_data; - let result = match login(&state, parameters).await { - Ok(result) => result, - Err(err) => { - tracing::warn!("login: internal error occurred {err:?}"); - schema::LoginResult::error(-1, String::from("Internal server error")) - } - }; + let result = login(&state, parameters).await.unwrap_or_else(|err| { + tracing::warn!("login: internal error occurred {err:?}"); + schema::LoginResult::error(-1, String::from("Internal server error")) + }); Json(result.with_user_data(user_data)) } @@ -32,7 +29,7 @@ async fn login(state: &ServiceState, params: schema::LoginParameters) -> Result< Some(account) => { if let Some(ban_time_stamp) = account.ban_time_stamp { if time_util::unix_timestamp() < ban_time_stamp as u64 { - return Ok(schema::LoginResult::banned(String::from("You're banned MF"), ban_time_stamp as i64)); + return Ok(schema::LoginResult::banned(String::from("You're banned MF"), ban_time_stamp)); } } diff --git a/shorekeeper-http/src/lib.rs b/shorekeeper-http/src/lib.rs index e366d3b..9ae3725 100644 --- a/shorekeeper-http/src/lib.rs +++ b/shorekeeper-http/src/lib.rs @@ -24,6 +24,12 @@ pub struct Application { impl Application<()> { pub fn new() -> Self { + Default::default() + } +} + +impl Default for Application<()> { + fn default() -> Self { Self { router: Router::new(), state: (), diff --git a/shorekeeper-network/src/message.rs b/shorekeeper-network/src/message.rs index ee43db2..5aa19b5 100644 --- a/shorekeeper-network/src/message.rs +++ b/shorekeeper-network/src/message.rs @@ -15,7 +15,7 @@ impl ServiceMessage { w.write_u16::(self.rpc_id)?; w.write_u16::(self.message_id)?; w.write_u32::(self.data.len() as u32)?; - w.write(&self.data)?; + w.write_all(&self.data)?; Ok(()) } diff --git a/shorekeeper-network/src/server.rs b/shorekeeper-network/src/server.rs index bdeb161..1d6d66c 100644 --- a/shorekeeper-network/src/server.rs +++ b/shorekeeper-network/src/server.rs @@ -28,8 +28,7 @@ impl ServiceListener { for message in data .into_vec() .into_iter() - .map(|b| ServiceMessage::decode(b.as_ref())) - .flatten() + .flat_map(|b| ServiceMessage::decode(b.as_ref())) { let _ = sender.send(message).await; } diff --git a/shorekeeper-protocol/build.rs b/shorekeeper-protocol/build.rs index 23eec8f..14e3a4f 100644 --- a/shorekeeper-protocol/build.rs +++ b/shorekeeper-protocol/build.rs @@ -15,7 +15,7 @@ pub fn main() { let config_path = Path::new("proto/config.csv"); if config_path.exists() { println!("cargo:rerun-if-changed={config_file}"); - impl_proto_config(config_path, &Path::new("generated/proto_config.rs")).unwrap(); + impl_proto_config(config_path, Path::new("generated/proto_config.rs")).unwrap(); } let proto_file = "proto/shorekeeper.proto"; diff --git a/shorekeeper-protocol/src/message.rs b/shorekeeper-protocol/src/message.rs index 3566dc6..87fd3c6 100644 --- a/shorekeeper-protocol/src/message.rs +++ b/shorekeeper-protocol/src/message.rs @@ -92,7 +92,7 @@ impl Message { let recv_crc = r.read_u32::()?; let mut payload = vec![0u8; src.len() - r.position() as usize].into_boxed_slice(); - r.read(&mut payload)?; + let _ = r.read(&mut payload)?; let calc_crc = crc32fast::hash(&payload); diff --git a/shorekeeper-protokey/src/lib.rs b/shorekeeper-protokey/src/lib.rs index 08a5f24..44cacb1 100644 --- a/shorekeeper-protokey/src/lib.rs +++ b/shorekeeper-protokey/src/lib.rs @@ -187,6 +187,6 @@ fn encrypt_aes256_ecb_pkcs7( ) -> Result, InvalidLength> { let cipher = Aes256::new_from_slice(session_key)?; Ok(cipher - .encrypt_padded_vec::(&data[..]) + .encrypt_padded_vec::(data) .into_boxed_slice()) }