use core::any::TypeId;
use bevy_ptr::{MovingPtr, OwningPtr};
use core::mem::MaybeUninit;
use variadics_please::all_tuples_enumerated;
use crate::{
bundle::{Bundle, BundleFromComponents, DynamicBundle, NoBundleEffect},
component::{Component, ComponentId, Components, ComponentsRegistrator, StorageType},
world::EntityWorldMut,
};
unsafe impl<C: Component> Bundle for C {
fn component_ids(components: &mut ComponentsRegistrator, ids: &mut impl FnMut(ComponentId)) {
ids(components.register_component::<C>());
}
fn get_component_ids(components: &Components, ids: &mut impl FnMut(Option<ComponentId>)) {
ids(components.get_id(TypeId::of::<C>()));
}
}
unsafe impl<C: Component> BundleFromComponents for C {
unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> Self
where
F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a>,
Self: Sized,
{
let ptr = func(ctx);
unsafe { ptr.read() }
}
}
impl<C: Component> DynamicBundle for C {
type Effect = ();
#[inline]
unsafe fn get_components(
ptr: MovingPtr<'_, Self>,
func: &mut impl FnMut(StorageType, OwningPtr<'_>),
) -> Self::Effect {
func(C::STORAGE_TYPE, OwningPtr::from(ptr));
}
#[inline]
unsafe fn apply_effect(_ptr: MovingPtr<'_, MaybeUninit<Self>>, _entity: &mut EntityWorldMut) {}
}
macro_rules! tuple_impl {
($(#[$meta:meta])* $(($index:tt, $name: ident, $alias: ident)),*) => {
#[expect(
clippy::allow_attributes,
reason = "This is a tuple-related macro; as such, the lints below may not always apply."
)]
#[allow(
unused_mut,
unused_variables,
reason = "Zero-length tuples won't use any of the parameters."
)]
$(#[$meta])*
unsafe impl<$($name: Bundle),*> Bundle for ($($name,)*) {
fn component_ids(components: &mut ComponentsRegistrator, ids: &mut impl FnMut(ComponentId)){
$(<$name as Bundle>::component_ids(components, ids);)*
}
fn get_component_ids(components: &Components, ids: &mut impl FnMut(Option<ComponentId>)){
$(<$name as Bundle>::get_component_ids(components, ids);)*
}
}
#[expect(
clippy::allow_attributes,
reason = "This is a tuple-related macro; as such, the lints below may not always apply."
)]
#[allow(
unused_mut,
unused_variables,
reason = "Zero-length tuples won't use any of the parameters."
)]
$(#[$meta])*
unsafe impl<$($name: BundleFromComponents),*> BundleFromComponents for ($($name,)*) {
#[allow(
clippy::unused_unit,
reason = "Zero-length tuples will generate a function body equivalent to `()`; however, this macro is meant for all applicable tuples, and as such it makes no sense to rewrite it just for that case."
)]
unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> Self
where
F: FnMut(&mut T) -> OwningPtr<'_>
{
#[allow(
unused_unsafe,
reason = "Zero-length tuples will not run anything in the unsafe block. Additionally, rewriting this to move the () outside of the unsafe would require putting the safety comment inside the tuple, hurting readability of the code."
)]
unsafe { ($(<$name as BundleFromComponents>::from_components(ctx, func),)*) }
}
}
#[expect(
clippy::allow_attributes,
reason = "This is a tuple-related macro; as such, the lints below may not always apply."
)]
#[allow(
unused_mut,
unused_variables,
reason = "Zero-length tuples won't use any of the parameters."
)]
$(#[$meta])*
impl<$($name: Bundle),*> DynamicBundle for ($($name,)*) {
type Effect = ($($name::Effect,)*);
#[allow(
clippy::unused_unit,
reason = "Zero-length tuples will generate a function body equivalent to `()`; however, this macro is meant for all applicable tuples, and as such it makes no sense to rewrite it just for that case."
)]
#[inline(always)]
unsafe fn get_components(ptr: MovingPtr<'_, Self>, func: &mut impl FnMut(StorageType, OwningPtr<'_>)) {
bevy_ptr::deconstruct_moving_ptr!({
let tuple { $($index: $alias,)* } = ptr;
});
$( $name::get_components($alias, func); )*
}
#[allow(
clippy::unused_unit,
reason = "Zero-length tuples will generate a function body equivalent to `()`; however, this macro is meant for all applicable tuples, and as such it makes no sense to rewrite it just for that case."
)]
#[inline(always)]
unsafe fn apply_effect(ptr: MovingPtr<'_, MaybeUninit<Self>>, entity: &mut EntityWorldMut) {
bevy_ptr::deconstruct_moving_ptr!({
let MaybeUninit::<tuple> { $($index: $alias,)* } = ptr;
});
$( $name::apply_effect($alias, entity); )*
}
}
$(#[$meta])*
impl<$($name: NoBundleEffect),*> NoBundleEffect for ($($name,)*) {}
}
}
all_tuples_enumerated!(
#[doc(fake_variadic)]
tuple_impl,
0,
15,
B,
field_
);