Skip to content

Commit 8a8293b

Browse files
committed
Renamed Entity::new to Entity::from_raw (#3465)
# Objective - Rename `Entity::new(id: u32)` to `Entity::from_raw(id: u32)`. - Add further documentation. - fixes #3108 ## Solution - Renamed `Entity::new(id: u32)` to `Entity::from_raw(id: u32)`. - Docs extended. I derived the examples from the discussion of issue #3108 . The [first case](#3108 (comment)) mentioned in the linked issue is quite obvious but the [second one](#3108 (comment)) probably needs further explanation. Co-authored-by: r4gus <[email protected]>
1 parent fd743ec commit 8a8293b

File tree

9 files changed

+64
-25
lines changed

9 files changed

+64
-25
lines changed

benches/benches/bevy_ecs/commands.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn get_or_spawn(criterion: &mut Criterion) {
250250
let mut commands = Commands::new(&mut command_queue, &world);
251251
for i in 0..10_000 {
252252
commands
253-
.get_or_spawn(Entity::new(i))
253+
.get_or_spawn(Entity::from_raw(i))
254254
.insert_bundle((Matrix::default(), Vec3::default()));
255255
}
256256
command_queue.apply(&mut world);
@@ -265,7 +265,7 @@ fn get_or_spawn(criterion: &mut Criterion) {
265265
let mut commands = Commands::new(&mut command_queue, &world);
266266
let mut values = Vec::with_capacity(10_000);
267267
for i in 0..10_000 {
268-
values.push((Entity::new(i), (Matrix::default(), Vec3::default())));
268+
values.push((Entity::from_raw(i), (Matrix::default(), Vec3::default())));
269269
}
270270
commands.insert_or_spawn_batch(values);
271271
command_queue.apply(&mut world);

benches/benches/bevy_ecs/world_get.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn world_entity(criterion: &mut Criterion) {
3737

3838
bencher.iter(|| {
3939
for i in 0..entity_count {
40-
let entity = Entity::new(i);
40+
let entity = Entity::from_raw(i);
4141
black_box(world.entity(entity));
4242
}
4343
});
@@ -58,7 +58,7 @@ fn world_get(criterion: &mut Criterion) {
5858

5959
bencher.iter(|| {
6060
for i in 0..entity_count {
61-
let entity = Entity::new(i);
61+
let entity = Entity::from_raw(i);
6262
assert!(world.get::<Table>(entity).is_some());
6363
}
6464
});
@@ -68,7 +68,7 @@ fn world_get(criterion: &mut Criterion) {
6868

6969
bencher.iter(|| {
7070
for i in 0..entity_count {
71-
let entity = Entity::new(i);
71+
let entity = Entity::from_raw(i);
7272
assert!(world.get::<Sparse>(entity).is_some());
7373
}
7474
});
@@ -90,7 +90,7 @@ fn world_query_get(criterion: &mut Criterion) {
9090

9191
bencher.iter(|| {
9292
for i in 0..entity_count {
93-
let entity = Entity::new(i);
93+
let entity = Entity::from_raw(i);
9494
assert!(query.get(&world, entity).is_ok());
9595
}
9696
});
@@ -101,7 +101,7 @@ fn world_query_get(criterion: &mut Criterion) {
101101

102102
bencher.iter(|| {
103103
for i in 0..entity_count {
104-
let entity = Entity::new(i);
104+
let entity = Entity::from_raw(i);
105105
assert!(query.get(&world, entity).is_ok());
106106
}
107107
});

crates/bevy_ecs/src/entity/mod.rs

+43-4
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,54 @@ pub enum AllocAtWithoutReplacement {
5757
}
5858

5959
impl Entity {
60-
/// Creates a new entity reference with a generation of 0.
60+
/// Creates a new entity reference with the specified `id` and a generation of 0.
6161
///
6262
/// # Note
6363
///
64-
/// Spawning a specific `entity` value is rarely the right choice. Most apps should favor
64+
/// Spawning a specific `entity` value is __rarely the right choice__. Most apps should favor
6565
/// [`Commands::spawn`](crate::system::Commands::spawn). This method should generally
6666
/// only be used for sharing entities across apps, and only when they have a scheme
6767
/// worked out to share an ID space (which doesn't happen by default).
68-
pub fn new(id: u32) -> Entity {
68+
///
69+
/// In general, one should not try to synchronize the ECS by attempting to ensure that
70+
/// `Entity` lines up between instances, but instead insert a secondary identifier as
71+
/// a component.
72+
///
73+
/// There are still some use cases where it might be appropriate to use this function
74+
/// externally.
75+
///
76+
/// ## Examples
77+
///
78+
/// Initializing a collection (e.g. `array` or `Vec`) with a known size:
79+
///
80+
/// ```no_run
81+
/// # use bevy_ecs::prelude::*;
82+
/// // Create a new array of size 10 and initialize it with (invalid) entities.
83+
/// let mut entities: [Entity; 10] = [Entity::from_raw(0); 10];
84+
///
85+
/// // ... replace the entities with valid ones.
86+
/// ```
87+
///
88+
/// Deriving `Reflect` for a component that has an `Entity` field:
89+
///
90+
/// ```no_run
91+
/// # use bevy_ecs::{prelude::*, component::*};
92+
/// # use bevy_reflect::Reflect;
93+
/// #[derive(Reflect, Component)]
94+
/// #[reflect(Component)]
95+
/// pub struct MyStruct {
96+
/// pub entity: Entity,
97+
/// }
98+
///
99+
/// impl FromWorld for MyStruct {
100+
/// fn from_world(_world: &mut World) -> Self {
101+
/// Self {
102+
/// entity: Entity::from_raw(u32::MAX),
103+
/// }
104+
/// }
105+
/// }
106+
/// ```
107+
pub fn from_raw(id: u32) -> Entity {
69108
Entity { id, generation: 0 }
70109
}
71110

@@ -120,7 +159,7 @@ impl SparseSetIndex for Entity {
120159
}
121160

122161
fn get_sparse_set_index(value: usize) -> Self {
123-
Entity::new(value as u32)
162+
Entity::from_raw(value as u32)
124163
}
125164
}
126165

crates/bevy_ecs/src/entity/serde.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ impl<'de> Visitor<'de> for EntityVisitor {
3232
where
3333
E: serde::de::Error,
3434
{
35-
Ok(Entity::new(v))
35+
Ok(Entity::from_raw(v))
3636
}
3737
}

crates/bevy_ecs/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1307,8 +1307,8 @@ mod tests {
13071307
let mut world_a = World::new();
13081308
let world_b = World::new();
13091309
let mut query = world_a.query::<&A>();
1310-
let _ = query.get(&world_a, Entity::new(0));
1311-
let _ = query.get(&world_b, Entity::new(0));
1310+
let _ = query.get(&world_a, Entity::from_raw(0));
1311+
let _ = query.get(&world_b, Entity::from_raw(0));
13121312
}
13131313

13141314
#[test]
@@ -1531,7 +1531,7 @@ mod tests {
15311531
fn insert_or_spawn_batch() {
15321532
let mut world = World::default();
15331533
let e0 = world.spawn().insert(A(0)).id();
1534-
let e1 = Entity::new(1);
1534+
let e1 = Entity::from_raw(1);
15351535

15361536
let values = vec![(e0, (B(0), C)), (e1, (B(1), C))];
15371537

@@ -1568,7 +1568,7 @@ mod tests {
15681568
fn insert_or_spawn_batch_invalid() {
15691569
let mut world = World::default();
15701570
let e0 = world.spawn().insert(A(0)).id();
1571-
let e1 = Entity::new(1);
1571+
let e1 = Entity::from_raw(1);
15721572
let e2 = world.spawn().id();
15731573
let invalid_e2 = Entity {
15741574
generation: 1,

crates/bevy_ecs/src/storage/sparse_set.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,11 @@ mod tests {
448448
#[test]
449449
fn sparse_set() {
450450
let mut set = SparseSet::<Entity, Foo>::default();
451-
let e0 = Entity::new(0);
452-
let e1 = Entity::new(1);
453-
let e2 = Entity::new(2);
454-
let e3 = Entity::new(3);
455-
let e4 = Entity::new(4);
451+
let e0 = Entity::from_raw(0);
452+
let e1 = Entity::from_raw(1);
453+
let e2 = Entity::from_raw(2);
454+
let e3 = Entity::from_raw(3);
455+
let e4 = Entity::from_raw(4);
456456

457457
set.insert(e1, Foo(1));
458458
set.insert(e2, Foo(2));

crates/bevy_ecs/src/storage/table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ mod tests {
540540
let columns = &[component_id];
541541
let mut table = Table::with_capacity(0, columns.len());
542542
table.add_column(components.get_info(component_id).unwrap());
543-
let entities = (0..200).map(Entity::new).collect::<Vec<_>>();
543+
let entities = (0..200).map(Entity::from_raw).collect::<Vec<_>>();
544544
for entity in entities.iter() {
545545
// SAFE: we allocate and immediately set data afterwards
546546
unsafe {

crates/bevy_scene/src/dynamic_scene.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl DynamicScene {
8787
// or spawn a new entity with a transiently unique id if there is
8888
// no corresponding entry.
8989
let entity = *entity_map
90-
.entry(bevy_ecs::entity::Entity::new(scene_entity.entity))
90+
.entry(bevy_ecs::entity::Entity::from_raw(scene_entity.entity))
9191
.or_insert_with(|| world.spawn().id());
9292

9393
// Apply/ add each component to the given entity.

crates/bevy_transform/src/components/parent.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Parent(pub Entity);
1717
// better ways to handle cases like this.
1818
impl FromWorld for Parent {
1919
fn from_world(_world: &mut World) -> Self {
20-
Parent(Entity::new(u32::MAX))
20+
Parent(Entity::from_raw(u32::MAX))
2121
}
2222
}
2323

@@ -56,6 +56,6 @@ impl MapEntities for PreviousParent {
5656
// TODO: Better handle this case see `impl FromWorld for Parent`
5757
impl FromWorld for PreviousParent {
5858
fn from_world(_world: &mut World) -> Self {
59-
PreviousParent(Entity::new(u32::MAX))
59+
PreviousParent(Entity::from_raw(u32::MAX))
6060
}
6161
}

0 commit comments

Comments
 (0)