Skip to content

Commit

Permalink
Fix the clippy failure
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Sep 17, 2021
1 parent 35362b2 commit 3f65af8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions crates/bevy_ecs/src/world/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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()
}
}

Expand All @@ -33,6 +34,7 @@ mod tests {
fn world_ids_unique() {
let ids = std::iter::repeat_with(WorldId::new)
.take(50)
.map(Option::unwrap)
.collect::<Vec<_>>();
for (i, &id1) in ids.iter().enumerate() {
// For the first element, i is 0 - so skip 1
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 3f65af8

Please sign in to comment.