Skip to content

Commit

Permalink
Make builder types take and return Self
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanabenki committed Oct 2, 2023
1 parent 21518de commit 88ce316
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 111 deletions.
13 changes: 8 additions & 5 deletions crates/bevy_ecs/src/storage/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,13 +526,16 @@ impl TableBuilder {
}
}

pub fn add_column(&mut self, component_info: &ComponentInfo) {
#[must_use]
pub fn add_column(mut self, component_info: &ComponentInfo) -> Self {
self.columns.insert(
component_info.id(),
Column::with_capacity(component_info, self.capacity),
);
self
}

#[must_use]
pub fn build(self) -> Table {
Table {
columns: self.columns.into_immutable(),
Expand Down Expand Up @@ -865,7 +868,7 @@ impl Tables {
.or_insert_with(|| {
let mut table = TableBuilder::with_capacity(0, component_ids.len());
for component_id in component_ids {
table.add_column(components.get_info_unchecked(*component_id));
table = table.add_column(components.get_info_unchecked(*component_id));
}
tables.push(table.build());
(component_ids.to_vec(), TableId::new(tables.len() - 1))
Expand Down Expand Up @@ -929,9 +932,9 @@ mod tests {
let mut storages = Storages::default();
let component_id = components.init_component::<W<TableRow>>(&mut storages);
let columns = &[component_id];
let mut builder = TableBuilder::with_capacity(0, columns.len());
builder.add_column(components.get_info(component_id).unwrap());
let mut table = builder.build();
let mut table = TableBuilder::with_capacity(0, columns.len())
.add_column(components.get_info(component_id).unwrap())
.build();
let entities = (0..200).map(Entity::from_raw).collect::<Vec<_>>();
for entity in &entities {
// SAFETY: we allocate and immediately set data afterwards
Expand Down
18 changes: 8 additions & 10 deletions crates/bevy_scene/src/dynamic_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ impl DynamicScene {

/// Create a new dynamic scene from a given world.
pub fn from_world(world: &World) -> Self {
let mut builder = DynamicSceneBuilder::from_world(world);

builder.extract_entities(world.iter_entities().map(|entity| entity.id()));
builder.extract_resources();

builder.build()
DynamicSceneBuilder::from_world(world)
.extract_entities(world.iter_entities().map(|entity| entity.id()))
.extract_resources()
.build()
}

/// Write the resources, the dynamic entities, and their corresponding components to the given world.
Expand Down Expand Up @@ -210,10 +208,10 @@ mod tests {

// We then write this relationship to a new scene, and then write that scene back to the
// world to create another parent and child relationship
let mut scene_builder = DynamicSceneBuilder::from_world(&world);
scene_builder.extract_entity(original_parent_entity);
scene_builder.extract_entity(original_child_entity);
let scene = scene_builder.build();
let scene = DynamicSceneBuilder::from_world(&world)
.extract_entity(original_parent_entity)
.extract_entity(original_child_entity)
.build();
let mut entity_map = HashMap::default();
scene.write_to_world(&mut world, &mut entity_map).unwrap();

Expand Down
Loading

0 comments on commit 88ce316

Please sign in to comment.