From 3f65af876f82bc1b93dc3530c5912ba96bb69c62 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Fri, 17 Sep 2021 13:15:44 +0100 Subject: [PATCH] Fix the clippy failure --- crates/bevy_ecs/src/world/identifier.rs | 18 ++++++++++-------- crates/bevy_ecs/src/world/mod.rs | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/bevy_ecs/src/world/identifier.rs b/crates/bevy_ecs/src/world/identifier.rs index e5f99ae6ed8cbf..6e2b8efc0f2220 100644 --- a/crates/bevy_ecs/src/world/identifier.rs +++ b/crates/bevy_ecs/src/world/identifier.rs @@ -3,25 +3,26 @@ use std::sync::atomic::{AtomicUsize, Ordering}; #[derive(Copy, Clone, PartialEq, Eq, Debug)] // We use usize here because that is the largest `Atomic` we want to require /// A unique identifier for a [`super::World`]. +// Note that this *is* used by external crates as well as for internal safety checks pub struct WorldId(usize); /// The next [`WorldId`]. static MAX_WORLD_ID: AtomicUsize = AtomicUsize::new(0); impl WorldId { - /// Create a new, unique [`WorldId`] + /// Create a new, unique [`WorldId`]. Returns [`None`] if the supply of unique + /// [`WorldId`]s has been exhausted /// - /// # Panics - /// If [`usize::MAX`] [`WorldId`]s have been created - // This could be kept crate private, but external crates may wish to create some for their tests - pub fn new() -> Self { - let id = MAX_WORLD_ID + /// Please note that the [`WorldId`]s created from this method are unique across + /// time - if a given [`WorldId`] is [`Drop`]ped its value still cannot be reused + pub fn new() -> Option { + MAX_WORLD_ID // We use `Relaxed` here since this atomic only needs to be consistent with itself .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |val| { val.checked_add(1) }) - .expect("More `bevy` `World`s have been created than is supported"); - WorldId(id) + .map(WorldId) + .ok() } } @@ -33,6 +34,7 @@ mod tests { fn world_ids_unique() { let ids = std::iter::repeat_with(WorldId::new) .take(50) + .map(Option::unwrap) .collect::>(); for (i, &id1) in ids.iter().enumerate() { // For the first element, i is 0 - so skip 1 diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 33633d62a6211b..25c775d499d96c 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -51,7 +51,7 @@ pub struct World { impl Default for World { fn default() -> Self { Self { - id: WorldId::new(), + id: WorldId::new().expect("More `bevy` `World`s have been created than is supported"), entities: Default::default(), components: Default::default(), archetypes: Default::default(),