diff options
| author | Fuwn <[email protected]> | 2024-06-03 15:37:20 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-06-03 15:37:20 -0700 |
| commit | 714d9b3cf1851ffe02424f0742126d3e82c76ccb (patch) | |
| tree | c9ab6b0f57ec91e351bcc5cadf33de925b136048 /crates/whirl_server/src | |
| parent | format: rustfmt with new rules (diff) | |
| download | whirl-714d9b3cf1851ffe02424f0742126d3e82c76ccb.tar.xz whirl-714d9b3cf1851ffe02424f0742126d3e82c76ccb.zip | |
refactor(crates): update idioms
Diffstat (limited to 'crates/whirl_server/src')
| -rw-r--r-- | crates/whirl_server/src/cmd/commands/appear_actor.rs | 4 | ||||
| -rw-r--r-- | crates/whirl_server/src/cmd/commands/register_object_id.rs | 2 | ||||
| -rw-r--r-- | crates/whirl_server/src/cmd/structure.rs | 6 | ||||
| -rw-r--r-- | crates/whirl_server/src/distributor.rs | 10 | ||||
| -rw-r--r-- | crates/whirl_server/src/hub.rs | 8 | ||||
| -rw-r--r-- | crates/whirl_server/src/interaction/shared.rs | 1 | ||||
| -rw-r--r-- | crates/whirl_server/src/lib.rs | 18 | ||||
| -rw-r--r-- | crates/whirl_server/src/net/network_property.rs | 5 | ||||
| -rw-r--r-- | crates/whirl_server/src/packet_parser.rs | 4 |
9 files changed, 28 insertions, 30 deletions
diff --git a/crates/whirl_server/src/cmd/commands/appear_actor.rs b/crates/whirl_server/src/cmd/commands/appear_actor.rs index 836c681..6100fcc 100644 --- a/crates/whirl_server/src/cmd/commands/appear_actor.rs +++ b/crates/whirl_server/src/cmd/commands/appear_actor.rs @@ -25,8 +25,8 @@ impl Creatable for AppearActor { command.put_i8(Command::ApprActr as i32 as i8); // Type // Content - command.put_i8(self.short_object_id as i8); // ObjId, why is it here? Worlds... - command.put_u16(self.room_id as u16); // Room ID + command.put_i8(self.short_object_id); // ObjId, why is it here? Worlds... + command.put_u16(self.room_id); // Room ID command.put_u16(self.x as u16); // X command.put_u16(self.y as u16); // Y command.put_u16(self.z as u16); // Z diff --git a/crates/whirl_server/src/cmd/commands/register_object_id.rs b/crates/whirl_server/src/cmd/commands/register_object_id.rs index 7791ae2..bdf4bca 100644 --- a/crates/whirl_server/src/cmd/commands/register_object_id.rs +++ b/crates/whirl_server/src/cmd/commands/register_object_id.rs @@ -23,7 +23,7 @@ impl Creatable for RegisterObjectId { // Content command.put_u8(self.long_object_id.len() as u8); // Long object ID length command.put_slice(self.long_object_id.as_bytes()); // Long object ID - command.put_i8(self.short_object_id as i8); // Short object ID + command.put_i8(self.short_object_id); // Short object ID // Length let mut command_as_vec = command.to_vec(); diff --git a/crates/whirl_server/src/cmd/structure.rs b/crates/whirl_server/src/cmd/structure.rs index 54d47ed..de6567e 100644 --- a/crates/whirl_server/src/cmd/structure.rs +++ b/crates/whirl_server/src/cmd/structure.rs @@ -1,6 +1,7 @@ // Copyright (C) 2021-2021 The Whirlsplash Collective // SPDX-License-Identifier: GPL-3.0-only +#[derive(Default)] pub struct Command { pub length: i32, pub obj_id: i32, @@ -32,7 +33,7 @@ impl Command { body: vec![], }; if command.length > 3 { - command.body = data[3..].to_owned(); + data[3..].clone_into(&mut command.body); } command_set.push(command); @@ -44,6 +45,3 @@ impl Command { command_set } } -impl Default for Command { - fn default() -> Self { Self { length: 0, obj_id: 0, id: 0, body: vec![] } } -} diff --git a/crates/whirl_server/src/distributor.rs b/crates/whirl_server/src/distributor.rs index cf9acbf..e89b8d8 100644 --- a/crates/whirl_server/src/distributor.rs +++ b/crates/whirl_server/src/distributor.rs @@ -67,7 +67,7 @@ impl Server for Distributor { result = peer.bytes.next() => match result { Some(Ok(msg)) => { for msg in parse_commands_from_packet(msg) { - match num_traits::FromPrimitive::from_i32(msg.get(2).unwrap().to_owned() as i32) { + match num_traits::FromPrimitive::from_i32(i32::from(msg.get(2).unwrap().to_owned())) { Some(Command::PropReq) => { debug!("received property request from client"); @@ -76,9 +76,9 @@ impl Server for Distributor { trace!("sent property update to client"); } Some(Command::SessInit) => { - username = (*crate::net::property_list::PropertyList::from_bytes(msg[3..] + username = crate::net::property_list::PropertyList::from_bytes(msg[3..] .to_vec()) - .find(VAR_USERNAME)).value.to_string(); + .find(VAR_USERNAME).value.to_string(); debug!("received session initialization from {}", username); @@ -114,13 +114,13 @@ impl Server for Distributor { trace!("found room: {}", room.room_name); room_id = position; } else { - room_ids.push((&*room.room_name).to_string()); + room_ids.push((*room.room_name).to_string()); room_id = room_ids.iter().position(|r| r == &room.room_name).unwrap(); trace!("inserted room {}: {}", room.room_name, room_id); } peer.bytes.get_mut().write_all(&RedirectId { - room_name: (&*room.room_name).to_string(), + room_name: (*room.room_name).to_string(), room_number: room_id as i8, }.create()).await?; trace!("sent redirect id to {}: {}", username, room.room_name); diff --git a/crates/whirl_server/src/hub.rs b/crates/whirl_server/src/hub.rs index af4e656..f1a8120 100644 --- a/crates/whirl_server/src/hub.rs +++ b/crates/whirl_server/src/hub.rs @@ -70,7 +70,7 @@ impl Server for Hub { Some(Ok(msg)) => { // trace!("got some bytes: {:?}", &msg); for msg in parse_commands_from_packet(msg) { - match num_traits::FromPrimitive::from_i32(msg.get(2).unwrap().to_owned() as i32) { + match num_traits::FromPrimitive::from_i32(i32::from(msg.get(2).unwrap().to_owned())) { Some(Command::PropReq) => { debug!("received property request from client"); @@ -79,9 +79,9 @@ impl Server for Hub { trace!("sent property update to client"); } Some(Command::SessInit) => { - username = (*crate::net::property_list::PropertyList::from_bytes(msg[3..] + username = crate::net::property_list::PropertyList::from_bytes(msg[3..] .to_vec()) - .find(VAR_USERNAME)).value.to_string(); + .find(VAR_USERNAME).value.to_string(); debug!("received session initialization from {}", username); @@ -142,7 +142,7 @@ impl Server for Hub { { state.lock().await.broadcast(&Text { - sender: (&*username).to_string(), + sender: (*username).to_string(), content: text.content.clone(), }.create()).await; } diff --git a/crates/whirl_server/src/interaction/shared.rs b/crates/whirl_server/src/interaction/shared.rs index fc8674e..4e1d158 100644 --- a/crates/whirl_server/src/interaction/shared.rs +++ b/crates/whirl_server/src/interaction/shared.rs @@ -9,6 +9,7 @@ pub struct Shared { impl Shared { pub fn new() -> Self { Self { peers: HashMap::new() } } + #[allow(clippy::unused_async)] pub async fn broadcast(&mut self, message: &[u8]) { for peer in &mut self.peers { peer.1.send(BytesMut::from(message)).unwrap(); diff --git a/crates/whirl_server/src/lib.rs b/crates/whirl_server/src/lib.rs index e8fdb6f..410522b 100644 --- a/crates/whirl_server/src/lib.rs +++ b/crates/whirl_server/src/lib.rs @@ -18,7 +18,13 @@ html_logo_url = "https://raw.githubusercontent.com/Whirlsplash/assets/master/Whirl.png", html_favicon_url = "https://raw.githubusercontent.com/Whirlsplash/assets/master/Whirl.png" )] -#![allow(non_local_definitions, dead_code)] +#![allow( + non_local_definitions, + dead_code, + clippy::cast_possible_truncation, + clippy::cast_sign_loss, + clippy::cast_possible_wrap +)] #[macro_use] extern crate log; #[macro_use] extern crate async_trait; @@ -53,7 +59,7 @@ pub enum ServerType { // https://stackoverflow.com/a/32712140/14452787 impl fmt::Display for ServerType { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self) + write!(f, "{self:?}") } } @@ -123,7 +129,7 @@ pub mod make { pub fn distributor() -> JoinHandle<()> { tokio::spawn(async move { crate::distributor::Distributor::listen( - &*format!( + &format!( "{}:{}", Config::get().whirlsplash.ip, Config::get().distributor.port @@ -144,11 +150,7 @@ pub mod make { pub fn hub() -> JoinHandle<()> { tokio::spawn(async move { crate::hub::Hub::listen( - &*format!( - "{}:{}", - Config::get().whirlsplash.ip, - Config::get().hub.port - ), + &format!("{}:{}", Config::get().whirlsplash.ip, Config::get().hub.port), ServerType::Room, ) .await diff --git a/crates/whirl_server/src/net/network_property.rs b/crates/whirl_server/src/net/network_property.rs index ef793bc..390412e 100644 --- a/crates/whirl_server/src/net/network_property.rs +++ b/crates/whirl_server/src/net/network_property.rs @@ -1,7 +1,7 @@ // Copyright (C) 2021-2021 The Whirlsplash Collective // SPDX-License-Identifier: GPL-3.0-only -#[derive(Clone)] +#[derive(Clone, Default)] pub struct NetworkProperty { pub prop_id: i32, pub value: String, @@ -9,6 +9,3 @@ pub struct NetworkProperty { impl NetworkProperty { pub fn _new() -> Self { Self::default() } } -impl Default for NetworkProperty { - fn default() -> Self { Self { prop_id: 0, value: "".to_string() } } -} diff --git a/crates/whirl_server/src/packet_parser.rs b/crates/whirl_server/src/packet_parser.rs index 13e9d0c..e0da32d 100644 --- a/crates/whirl_server/src/packet_parser.rs +++ b/crates/whirl_server/src/packet_parser.rs @@ -14,11 +14,11 @@ pub fn parse_commands_from_packet(mut buffer: BytesMut) -> Vec<BytesMut> { let mut commands: Vec<BytesMut> = Vec::new(); trace!("initial buffer: {:?}, length: {}", buffer, buffer.len()); - let data_length = buffer.get(0).unwrap().to_owned() as usize; + let data_length = buffer.first().unwrap().to_owned() as usize; if buffer.len() > data_length { loop { trace!("loop: {:?}, length: {}", buffer, buffer.len()); - let command_length = buffer.get(0).unwrap().to_owned() as usize; + let command_length = buffer.first().unwrap().to_owned() as usize; commands.push(BytesMut::from(buffer.get(0..command_length).unwrap())); // Remove command from buffer |