Browse Source

remove bit of unsafe code

move_stuff_to_libpso
jake 6 months ago
parent
commit
cd31052b9a
  1. 17
      src/client/src/lib.rs

17
src/client/src/lib.rs

@ -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,
} }
@ -67,24 +69,19 @@ impl Clients {
.read() .read()
.await; .await;
let mut client_states: [std::mem::MaybeUninit<RwLockReadGuard<ClientState>>; N] = unsafe {
std::mem::MaybeUninit::uninit().assume_init()
};
let mut client_states = Vec::new();
for (cindex, client_id) in client_ids.iter().enumerate() {
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 {
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<T, anyhow::Error> pub async fn with_mut<'a, T, F>(&'a self, client_id: ClientId, func: F) -> Result<T, anyhow::Error>

Loading…
Cancel
Save