diff --git a/src/chunk.rs b/src/chunk.rs index fa1d939..731b827 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -76,6 +76,11 @@ impl Chunks { 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. /// /// If there is no chunk at the position, then `None` is returned. diff --git a/src/client.rs b/src/client.rs index af60405..16636a3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -90,6 +90,12 @@ impl Clients { 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 /// the ID is invalid, then `None` is returned. pub fn get(&self, client: ClientId) -> Option<&Client> { diff --git a/src/entity.rs b/src/entity.rs index b2c374b..23bbe06 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -146,6 +146,11 @@ impl Entities { 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 /// manner. /// diff --git a/src/player_list.rs b/src/player_list.rs index f51f2bd..9abfe30 100644 --- a/src/player_list.rs +++ b/src/player_list.rs @@ -40,7 +40,11 @@ impl PlayerLists { } } + /// 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) { let (key, pl) = self.slab.insert(PlayerList { state, @@ -54,14 +58,28 @@ impl PlayerLists { (PlayerListId(key), pl) } + /// Returns the number of player lists. pub fn len(&self) -> usize { 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 { 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 { self.slab.get_mut(&id.0) } diff --git a/src/slab.rs b/src/slab.rs index 1471956..b95e637 100644 --- a/src/slab.rs +++ b/src/slab.rs @@ -297,7 +297,7 @@ impl FusedIterator for IterMut<'_, T> {} impl Clone for ParIter<'_, T> { fn clone(&self) -> Self { - Self { slab: &self.slab } + Self { slab: self.slab } } } diff --git a/src/slab_rc.rs b/src/slab_rc.rs index b996569..40da6ce 100644 --- a/src/slab_rc.rs +++ b/src/slab_rc.rs @@ -29,7 +29,7 @@ impl Clone for Slot { } } -#[derive(Clone, Eq, Ord, Debug)] +#[derive(Clone, Eq, Debug)] pub struct Key(Arc); 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 { fn hash(&self, state: &mut H) { Arc::as_ptr(&self.0).hash(state) diff --git a/src/slab_versioned.rs b/src/slab_versioned.rs index e4ecced..86c039b 100644 --- a/src/slab_versioned.rs +++ b/src/slab_versioned.rs @@ -68,12 +68,12 @@ impl VersionedSlab { pub fn get(&self, key: Key) -> Option<&T> { 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> { 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 { diff --git a/src/world.rs b/src/world.rs index 03ec5e3..2c8e7ca 100644 --- a/src/world.rs +++ b/src/world.rs @@ -79,6 +79,11 @@ impl Worlds { 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 /// the ID is invalid, then `None` is returned. pub fn get(&self, world: WorldId) -> Option<&World> {