Skip to content

Full world cloning #17316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions benches/benches/bevy_ecs/entity_cloning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ fn set_reflect_clone_handler<B: Bundle + GetTypeRegistration>(world: &mut World)
// this bundle are saved.
let component_ids: Vec<_> = world.register_bundle::<B>().contributed_components().into();

let clone_handlers = world.get_component_clone_handlers_mut();

// Overwrite the clone handler for all components in the bundle to use `Reflect`, not `Clone`.
for component in component_ids {
clone_handlers.set_component_handler(component, ComponentCloneHandler::reflect_handler());
world.set_component_clone_handler(component, ComponentCloneHandler::reflect_handler());
}
}

Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_ecs/macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ pub fn derive_resource(input: TokenStream) -> TokenStream {

TokenStream::from(quote! {
impl #impl_generics #bevy_ecs_path::system::Resource for #struct_name #type_generics #where_clause {
fn get_component_clone_handler() -> #bevy_ecs_path::component::ComponentCloneHandler {
use #bevy_ecs_path::component::{ComponentCloneViaClone, ComponentCloneViaCopy, ComponentCloneBase};

(&&&#bevy_ecs_path::component::ComponentCloneSpecializationWrapper::<Self>::default())
.get_component_clone_handler()
}
}
})
}
Expand Down Expand Up @@ -168,7 +174,8 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
}

fn get_component_clone_handler() -> #bevy_ecs_path::component::ComponentCloneHandler {
use #bevy_ecs_path::component::{ComponentCloneViaClone, ComponentCloneBase};
use #bevy_ecs_path::component::{ComponentCloneViaClone, ComponentCloneViaCopy, ComponentCloneBase};

(&&&#bevy_ecs_path::component::ComponentCloneSpecializationWrapper::<Self>::default())
.get_component_clone_handler()
}
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_ecs/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub(crate) enum ComponentStatus {
}

/// Used in [`Edges`] to cache the result of inserting a bundle into the source archetype.
#[derive(Clone)]
pub(crate) struct ArchetypeAfterBundleInsert {
/// The target archetype after the bundle is inserted into the source archetype.
pub archetype_id: ArchetypeId,
Expand Down Expand Up @@ -194,7 +195,7 @@ impl BundleComponentStatus for SpawnBundleStatus {
/// yet.
///
/// [`World`]: crate::world::World
#[derive(Default)]
#[derive(Default, Clone)]
pub struct Edges {
insert_bundle: SparseArray<BundleId, ArchetypeAfterBundleInsert>,
remove_bundle: SparseArray<BundleId, Option<ArchetypeId>>,
Expand Down Expand Up @@ -306,6 +307,7 @@ impl Edges {
}

/// Metadata about an [`Entity`] in a [`Archetype`].
#[derive(Clone)]
pub struct ArchetypeEntity {
entity: Entity,
table_row: TableRow,
Expand Down Expand Up @@ -339,6 +341,7 @@ pub(crate) struct ArchetypeSwapRemoveResult {
/// Internal metadata for a [`Component`] within a given [`Archetype`].
///
/// [`Component`]: crate::component::Component
#[derive(Clone)]
struct ArchetypeComponentInfo {
storage_type: StorageType,
archetype_component_id: ArchetypeComponentId,
Expand Down Expand Up @@ -367,6 +370,7 @@ bitflags::bitflags! {
///
/// [`World`]: crate::world::World
/// [module level documentation]: crate::archetype
#[derive(Clone)]
pub struct Archetype {
id: ArchetypeId,
table_id: TableId,
Expand Down Expand Up @@ -720,7 +724,7 @@ impl ArchetypeGeneration {
}
}

#[derive(Hash, PartialEq, Eq)]
#[derive(Clone, Hash, PartialEq, Eq)]
struct ArchetypeComponents {
table_components: Box<[ComponentId]>,
sparse_set_components: Box<[ComponentId]>,
Expand Down Expand Up @@ -776,6 +780,7 @@ pub type ComponentIndex = HashMap<ComponentId, HashMap<ArchetypeId, ArchetypeRec
///
/// [`World`]: crate::world::World
/// [module level documentation]: crate::archetype
#[derive(Clone)]
pub struct Archetypes {
pub(crate) archetypes: Vec<Archetype>,
archetype_component_count: usize,
Expand All @@ -786,6 +791,7 @@ pub struct Archetypes {
}

/// Metadata about how a component is stored in an [`Archetype`].
#[derive(Clone)]
pub struct ArchetypeRecord {
/// Index of the component in the archetype's [`Table`](crate::storage::Table),
/// or None if the component is a sparse set component.
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ pub enum InsertMode {
/// Stores metadata associated with a specific type of [`Bundle`] for a given [`World`].
///
/// [`World`]: crate::world::World
#[derive(Clone)]
pub struct BundleInfo {
id: BundleId,
/// The list of all components contributed by the bundle (including Required Components). This is in
Expand Down Expand Up @@ -1433,7 +1434,7 @@ impl<'w> BundleSpawner<'w> {
}

/// Metadata for bundles. Stores a [`BundleInfo`] for each type of [`Bundle`] in a given world.
#[derive(Default)]
#[derive(Default, Clone)]
pub struct Bundles {
bundle_infos: Vec<BundleInfo>,
/// Cache static [`BundleId`]
Expand Down
Loading
Loading