Skip to content

Make Ref copyable#23549

Open
ItsDoot wants to merge 4 commits intobevyengine:mainfrom
ItsDoot:ecs/ref
Open

Make Ref copyable#23549
ItsDoot wants to merge 4 commits intobevyengine:mainfrom
ItsDoot:ecs/ref

Conversation

@ItsDoot
Copy link
Copy Markdown
Contributor

@ItsDoot ItsDoot commented Mar 28, 2026

Objective

Trying to write a bevy_immediate widget with the following signature:

fn meter<T: Display>(self, name: impl Into<String>, current: Ref<T>, max: Ref<T>) -> Self;

Where it would be called like so (pseudocode):

#[derive(Component)]
struct Health {
    current: f32,
    max: f32
}

fn render(ui: &mut ImmEntity, health: Ref<Health>) {
    ui.meter("Health", health.map(|v| &v.current), health.map(|v| &v.max));
}

However, this results in a compiler error as there's currently no way to split borrows on a Ref<T> (there is no Ref::clone).

Solution

  • Make Ref both Clone and Copy, easily allowing split borrows.

Note: Res<T> is not Clone or Copy to make cloning the T out easier, but comparatively Ref<T> is much less commonly used (users primarily write &T in their query). And even further, I believe the amount of users who both use Ref<T> and who need to clone the T out will be quite small (we don't even have a Ref::clone inherent function, if that proves anything).

Alternatively, we could add Ref::clone as an inherent function, but the UX on that isn't great.

Testing

The rust compiler keeps us in check here.

@ItsDoot ItsDoot added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 28, 2026
@github-project-automation github-project-automation bot moved this to Needs SME Triage in ECS Mar 28, 2026
@SkiFire13
Copy link
Copy Markdown
Contributor

Note that this is technically a breaking change since it will break code calling .clone() on it (as you already mentioned).

I also wonder if we can improve the situation by e.g. adding a separate method to copy a Ref<T> and making some methods on it take &self instead of self (for instance your example would work if map took &self)

@ItsDoot ItsDoot added the M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Mar 28, 2026
@ItsDoot
Copy link
Copy Markdown
Contributor Author

ItsDoot commented Mar 28, 2026

Note that this is technically a breaking change

Added a migration guide.

@ItsDoot
Copy link
Copy Markdown
Contributor Author

ItsDoot commented Mar 29, 2026

Additionally, without Copy/Clone it would need Ref::reborrow to go from &Ref -> Ref, which would be a bit odd since EntityRef doesn't have or need a reborrow function.

@alice-i-cecile alice-i-cecile added the X-Contentious There are nontrivial implications that should be thought through label Mar 29, 2026
@alice-i-cecile
Copy link
Copy Markdown
Member

alice-i-cecile commented Mar 29, 2026

Initial impressions on this are positive: I think this is a reasonable tradeoff. Needs a comment about the inconsistency though.

Co-authored-by: Christian Hughes <9044780+ItsDoot@users.noreply.github.com>
@alice-i-cecile
Copy link
Copy Markdown
Member

@PPakalns, can you take a look at this quick? Is this pattern reasonable to use downstream in bevy_immediate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Contentious There are nontrivial implications that should be thought through

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

3 participants