Skip to content

Conversation

janis-bhm
Copy link
Contributor

@janis-bhm janis-bhm commented Oct 7, 2025

(I messed up by renaming the branch and I couldn't figure out how to change the branch and reopen #20984)
relevant issue: #20976

Solution

In order to spawn bundles inside MovingPtrs alongside other bundles, we need to be able to name MovingPtr as a bundle, which the new associated type Name on Bundle allows. This type is basically a canonicalized bundle name-type that allows types like SpawnRelatedBundle to appear to the Bundles as if they were the bundle they are wrapping: R::RelationshipTarget. (I need to write tests for this).

Other parts of the engine, like observers, rely on Bundle being 'static in order to pass around the type without being constrained by lifetimes, which is why I've renamed Bundle to BundleImpl and made trait Bundle: BundleImpl + 'static.
Not sure how much I like this personally.
Observers only use the associated trait functions of Bundle which shouldn't be affected by the lifetime of the type implementing Bundle (I think).
An alternative would be to enforce Bundle::Name to impl Bundle itself (which it does, since it's just a tuple of bundles) and have the same components/component order as Self so that Bundles methods are available 'static (though using DynamicBundle this way would certainly be unsafe). That could allow types carrying Bundles in phantom datas to use the associated functions for component order and ids.

Alternative Solutions

@james7132 pointed out the typeid crate, which is already in the dependency graph, and which allows getting TypeIds from non-static types, notably at the cost of dynamic dispatch. This could allow for a simple trait method that returns the type id rather than using the associated type Name.
Note:
As far as I can tell, rustc can inline the dynamic dispatch here into a static call to core::any::TypeId::of, meaning there is potentially no added cost to using typeid.

Click to expand
fn main() {
    struct X {
        a: u32,
        b: u64,
    }
    let x = X { a: 1, b: 2 };
    move_as_ptr!(x);
    let _x = black_box(get_typeid_of(x));
    assert_eq!(TypeId::of::<X>(), _x);
}

fn get_typeid_of<T: Debug>(_: T) -> TypeId {
    typeid::of::<T>()
}

disassembly:

playground::main:
	.cfi_startproc
	sub rsp, 88
	.cfi_def_cfa_offset 96
	movups xmm0, xmmword ptr [rip + .Lanon.e23240f1cfa1af806bfe11aec7d94188.5]
	movaps xmmword ptr [rsp], xmm0
	mov rax, rsp
	#APP
	#NO_APP
	movups xmm0, xmmword ptr [rip + .Lanon.e23240f1cfa1af806bfe11aec7d94188.0]
	movaps xmmword ptr [rsp + 16], xmm0
	movdqa xmm0, xmmword ptr [rsp]
	pcmpeqb xmm0, xmmword ptr [rip + .LCPI0_0]
	pmovmskb eax, xmm0
	cmp eax, 65535
	jne .LBB0_2
	add rsp, 88
	.cfi_def_cfa_offset 8
	ret

@janis-bhm janis-bhm added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Contentious There are nontrivial implications that should be thought through D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward S-Needs-Testing Testing must be done before this is safe to merge labels Oct 7, 2025
@james7132 james7132 added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Oct 8, 2025
Copy link
Contributor

github-actions bot commented Oct 8, 2025

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes.

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-Bug An unexpected or incorrect behavior C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Unsafe Touches with unsafe code in some way M-Needs-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 S-Needs-Testing Testing must be done before this is safe to merge X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants