Browse Source

remove bit of unsafe code

move_stuff_to_libpso
jake 5 months ago
parent
commit
cd31052b9a
  1. 27
      src/client/src/lib.rs

27
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<ClientState> {
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<T, anyhow::Error>
@ -66,25 +68,20 @@ impl Clients {
let clients = self.0
.read()
.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
.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<T, anyhow::Error>

Loading…
Cancel
Save