Fix clippy issues

This commit is contained in:
Ryan 2022-09-02 00:37:02 -07:00
parent f7a35f356e
commit dafc9a375a
8 changed files with 49 additions and 4 deletions

View file

@ -76,6 +76,11 @@ impl<C: Config> Chunks<C> {
self.chunks.len() self.chunks.len()
} }
/// Returns `true` if there are no loaded chunks.
pub fn is_empty(&self) -> bool {
self.chunks.len() == 0
}
/// Gets a shared reference to the chunk at the provided position. /// Gets a shared reference to the chunk at the provided position.
/// ///
/// If there is no chunk at the position, then `None` is returned. /// If there is no chunk at the position, then `None` is returned.

View file

@ -90,6 +90,12 @@ impl<C: Config> Clients<C> {
self.slab.len() self.slab.len()
} }
/// Returns `true` if there are no clients on the server. This includes
/// clients for which [`Client::is_disconnected`] returns true.
pub fn is_empty(&self) -> bool {
self.slab.len() == 0
}
/// Returns a shared reference to the client with the given ID. If /// Returns a shared reference to the client with the given ID. If
/// the ID is invalid, then `None` is returned. /// the ID is invalid, then `None` is returned.
pub fn get(&self, client: ClientId) -> Option<&Client<C>> { pub fn get(&self, client: ClientId) -> Option<&Client<C>> {

View file

@ -146,6 +146,11 @@ impl<C: Config> Entities<C> {
self.slab.len() self.slab.len()
} }
/// Returns `true` if there are no entities.
pub fn is_empty(&self) -> bool {
self.slab.len() == 0
}
/// Gets the [`EntityId`] of the entity with the given UUID in an efficient /// Gets the [`EntityId`] of the entity with the given UUID in an efficient
/// manner. /// manner.
/// ///

View file

@ -40,7 +40,11 @@ impl<C: Config> PlayerLists<C> {
} }
} }
/// Creates a new player list and returns an exclusive reference to it along
/// with its ID.
/// ///
/// The player list is automatically removed at the end of the tick once all
/// IDs to it have been dropped.
pub fn insert(&mut self, state: C::PlayerListState) -> (PlayerListId, &mut PlayerList<C>) { pub fn insert(&mut self, state: C::PlayerListState) -> (PlayerListId, &mut PlayerList<C>) {
let (key, pl) = self.slab.insert(PlayerList { let (key, pl) = self.slab.insert(PlayerList {
state, state,
@ -54,14 +58,28 @@ impl<C: Config> PlayerLists<C> {
(PlayerListId(key), pl) (PlayerListId(key), pl)
} }
/// Returns the number of player lists.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.slab.len() self.slab.len()
} }
/// Returns `true` if there are no player lists.
pub fn is_empty(&self) -> bool {
self.slab.len() == 0
}
/// Gets a shared reference to the player list with the given player list
/// ID.
///
/// This operation is infallible because [`PlayerListId`] is refcounted.
pub fn get(&self, id: &PlayerListId) -> &PlayerList<C> { pub fn get(&self, id: &PlayerListId) -> &PlayerList<C> {
self.slab.get(&id.0) self.slab.get(&id.0)
} }
/// Gets an exclusive reference to the player list with the given player
/// list ID.
///
/// This operation is infallible because [`PlayerListId`] is refcounted.
pub fn get_mut(&mut self, id: &PlayerListId) -> &mut PlayerList<C> { pub fn get_mut(&mut self, id: &PlayerListId) -> &mut PlayerList<C> {
self.slab.get_mut(&id.0) self.slab.get_mut(&id.0)
} }

View file

@ -297,7 +297,7 @@ impl<T> FusedIterator for IterMut<'_, T> {}
impl<T> Clone for ParIter<'_, T> { impl<T> Clone for ParIter<'_, T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { slab: &self.slab } Self { slab: self.slab }
} }
} }

View file

@ -29,7 +29,7 @@ impl<T: Clone> Clone for Slot<T> {
} }
} }
#[derive(Clone, Eq, Ord, Debug)] #[derive(Clone, Eq, Debug)]
pub struct Key(Arc<usize>); pub struct Key(Arc<usize>);
impl PartialEq for Key { impl PartialEq for Key {
@ -44,6 +44,12 @@ impl PartialOrd for Key {
} }
} }
impl Ord for Key {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
impl Hash for Key { impl Hash for Key {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
Arc::as_ptr(&self.0).hash(state) Arc::as_ptr(&self.0).hash(state)

View file

@ -68,12 +68,12 @@ impl<T> VersionedSlab<T> {
pub fn get(&self, key: Key) -> Option<&T> { pub fn get(&self, key: Key) -> Option<&T> {
let slot = self.slab.get(key.index as usize)?; let slot = self.slab.get(key.index as usize)?;
(slot.version == key.version).then(|| &slot.value) (slot.version == key.version).then_some(&slot.value)
} }
pub fn get_mut(&mut self, key: Key) -> Option<&mut T> { pub fn get_mut(&mut self, key: Key) -> Option<&mut T> {
let slot = self.slab.get_mut(key.index as usize)?; let slot = self.slab.get_mut(key.index as usize)?;
(slot.version == key.version).then(|| &mut slot.value) (slot.version == key.version).then_some(&mut slot.value)
} }
pub fn len(&self) -> usize { pub fn len(&self) -> usize {

View file

@ -79,6 +79,11 @@ impl<C: Config> Worlds<C> {
self.slab.len() self.slab.len()
} }
/// Returns `true` if there are no worlds on the server.
pub fn is_empty(&self) -> bool {
self.slab.len() == 0
}
/// Returns a shared reference to the world with the given ID. If /// Returns a shared reference to the world with the given ID. If
/// the ID is invalid, then `None` is returned. /// the ID is invalid, then `None` is returned.
pub fn get(&self, world: WorldId) -> Option<&World<C>> { pub fn get(&self, world: WorldId) -> Option<&World<C>> {