remove bit of unsafe code
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
jake 2023-11-30 00:49:55 -07:00
parent 04180d1d86
commit cd31052b9a

View File

@ -19,6 +19,8 @@ use shops::{WeaponShopItem, ToolShopItem, ArmorShopItem};
pub enum ClientError { pub enum ClientError {
#[error("not found {0}")] #[error("not found {0}")]
NotFound(ClientId), 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<ClientState> { pub async fn remove(&mut self, client_id: &ClientId) -> Option<ClientState> {
Some(self.0 Some(self.0
.write() .write()
.await .await
.remove(client_id)? .remove(client_id)?
.into_inner()) .into_inner())
} }
pub async fn with<'a, T, F>(&'a self, client_id: ClientId, func: F) -> Result<T, anyhow::Error> pub async fn with<'a, T, F>(&'a self, client_id: ClientId, func: F) -> Result<T, anyhow::Error>
@ -66,25 +68,20 @@ impl Clients {
let clients = self.0 let clients = self.0
.read() .read()
.await; .await;
let mut client_states: [std::mem::MaybeUninit<RwLockReadGuard<ClientState>>; 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 let c = clients
.get(client_id) .get(client_id)
.ok_or(ClientError::NotFound(*client_id))? .ok_or(ClientError::NotFound(*client_id))?
.read() .read()
.await; .await;
client_states[cindex].write(c); client_states.push(c);
} }
let client_states = unsafe { let result = func(client_states.try_into().map_err(|_| ClientError::WithManyFailed)?).await;
std::mem::transmute_copy(&client_states) Ok(result)
};
Ok(func(client_states).await)
} }
pub async fn with_mut<'a, T, F>(&'a self, client_id: ClientId, func: F) -> Result<T, anyhow::Error> pub async fn with_mut<'a, T, F>(&'a self, client_id: ClientId, func: F) -> Result<T, anyhow::Error>