From cd31052b9a8c330bec18a4b5a9695fea04549dae Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 30 Nov 2023 00:49:55 -0700 Subject: [PATCH] remove bit of unsafe code --- src/client/src/lib.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/client/src/lib.rs b/src/client/src/lib.rs index fc91a37..8afdc8b 100644 --- a/src/client/src/lib.rs +++ b/src/client/src/lib.rs @@ -19,6 +19,8 @@ use shops::{WeaponShopItem, ToolShopItem, ArmorShopItem}; pub enum ClientError { #[error("not found {0}")] NotFound(ClientId), + #[error("failed to get multiple clients")] + WithManyFailed, } @@ -35,10 +37,10 @@ impl Clients { pub async fn remove(&mut self, client_id: &ClientId) -> Option { Some(self.0 - .write() - .await - .remove(client_id)? - .into_inner()) + .write() + .await + .remove(client_id)? + .into_inner()) } pub async fn with<'a, T, F>(&'a self, client_id: ClientId, func: F) -> Result @@ -66,25 +68,20 @@ impl Clients { let clients = self.0 .read() .await; - - let mut client_states: [std::mem::MaybeUninit>; N] = unsafe { - std::mem::MaybeUninit::uninit().assume_init() - }; - for (cindex, client_id) in client_ids.iter().enumerate() { + let mut client_states = Vec::new(); + + for client_id in client_ids.iter() { let c = clients .get(client_id) .ok_or(ClientError::NotFound(*client_id))? .read() .await; - client_states[cindex].write(c); + client_states.push(c); } - let client_states = unsafe { - std::mem::transmute_copy(&client_states) - }; - - Ok(func(client_states).await) + let result = func(client_states.try_into().map_err(|_| ClientError::WithManyFailed)?).await; + Ok(result) } pub async fn with_mut<'a, T, F>(&'a self, client_id: ClientId, func: F) -> Result