Skip to content

Reflection allows users to clone arbitrary reflected components, but Reflect does not have a Clone bound #3372

@alice-i-cecile

Description

@alice-i-cecile

Problem

  1. ReflectComponent::copy_component allows users to copy any arbitrary component data to different entities, and across worlds, as long as it is reflect-able.
  2. Neither Reflect nor Component has a Clone bound, and so we can sneakily clone components that are not expecting to be cloned. I'm pretty sure this is ultimately done by just copying their raw memory.
  3. Components are effectively cloned in a way that bypasses the Clone method, which can cause issues with things like ref-counting (used for Handle).

Possible Solutions

  1. Give Reflect and/or Component a Clone bound. This fixes problem 2, but not problem 3, and may be painful in niche use cases (although I have not seen any concrete use cases for components that cannot be cloned raised, see Command to clone entities #1515).

Furthermore, not all fields of a component should be reflected. Cloning components with some cloneable field and defaulting the others is a reasonable strategy here, but does not permit a Clone bound.

  1. Use the appropriate Clone methods when they exist.

  2. Leave the existing behavior in place, and very clearly document what's going on and how to use it safely.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsA-ReflectionRuntime information about typesC-Code-QualityA section of code that is hard to understand or change

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions