Skip to content

Commit

Permalink
Rename init_component & friends (#15454)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #15451 

## Migration Guide

- `World::init_component` has been renamed to `register_component`.
- `World::init_component_with_descriptor` has been renamed to
`register_component_with_descriptor`.
- `World::init_bundle` has been renamed to `register_bundle`.
- `Components::init_component` has been renamed to `register_component`.
- `Components::init_component_with_descriptor` has been renamed to
`register_component_with_descriptor`.
- `Components::init_resource` has been renamed to `register_resource`.
- `Components::init_non_send` had been renamed to `register_non_send`.
  • Loading branch information
hooded-shrimp authored Sep 26, 2024
1 parent 5fcbdc1 commit 35d1086
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 122 deletions.
12 changes: 6 additions & 6 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ unsafe impl<C: Component> Bundle for C {
storages: &mut Storages,
ids: &mut impl FnMut(ComponentId),
) {
ids(components.init_component::<C>(storages));
ids(components.register_component::<C>(storages));
}

unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> Self
Expand Down Expand Up @@ -786,7 +786,7 @@ impl<'w> BundleInserter<'w> {
) -> Self {
let bundle_id = world
.bundles
.init_info::<T>(&mut world.components, &mut world.storages);
.register_info::<T>(&mut world.components, &mut world.storages);
// SAFETY: We just ensured this bundle exists
unsafe { Self::new_with_id(world, archetype_id, bundle_id, change_tick) }
}
Expand Down Expand Up @@ -1135,7 +1135,7 @@ impl<'w> BundleSpawner<'w> {
pub fn new<T: Bundle>(world: &'w mut World, change_tick: Tick) -> Self {
let bundle_id = world
.bundles
.init_info::<T>(&mut world.components, &mut world.storages);
.register_info::<T>(&mut world.components, &mut world.storages);
// SAFETY: we initialized this bundle_id in `init_info`
unsafe { Self::new_with_id(world, bundle_id, change_tick) }
}
Expand Down Expand Up @@ -1318,10 +1318,10 @@ impl Bundles {
self.bundle_ids.get(&type_id).cloned()
}

/// Initializes a new [`BundleInfo`] for a statically known type.
/// Registers a new [`BundleInfo`] for a statically known type.
///
/// Also initializes all the components in the bundle.
pub(crate) fn init_info<T: Bundle>(
/// Also registers all the components in the bundle.
pub(crate) fn register_info<T: Bundle>(
&mut self,
components: &mut Components,
storages: &mut Storages,
Expand Down
50 changes: 25 additions & 25 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ impl ComponentInfo {
/// [`World`].
///
/// Each time a new `Component` type is registered within a `World` using
/// e.g. [`World::init_component`] or [`World::init_component_with_descriptor`]
/// e.g. [`World::register_component`] or [`World::register_component_with_descriptor`]
/// or a Resource with e.g. [`World::init_resource`],
/// a corresponding `ComponentId` is created to track it.
///
Expand Down Expand Up @@ -837,16 +837,16 @@ pub struct Components {
}

impl Components {
/// Initializes a component of type `T` with this instance.
/// If a component of this type has already been initialized, this will return
/// Registers a component of type `T` with this instance.
/// If a component of this type has already been registered, this will return
/// the ID of the pre-existing component.
///
/// # See also
///
/// * [`Components::component_id()`]
/// * [`Components::init_component_with_descriptor()`]
/// * [`Components::register_component_with_descriptor()`]
#[inline]
pub fn init_component<T: Component>(&mut self, storages: &mut Storages) -> ComponentId {
pub fn register_component<T: Component>(&mut self, storages: &mut Storages) -> ComponentId {
let mut registered = false;
let id = {
let Components {
Expand All @@ -856,7 +856,7 @@ impl Components {
} = self;
let type_id = TypeId::of::<T>();
*indices.entry(type_id).or_insert_with(|| {
let id = Components::init_component_inner(
let id = Components::register_component_inner(
components,
storages,
ComponentDescriptor::new::<T>(),
Expand All @@ -875,7 +875,7 @@ impl Components {
id
}

/// Initializes a component described by `descriptor`.
/// Registers a component described by `descriptor`.
///
/// ## Note
///
Expand All @@ -885,17 +885,17 @@ impl Components {
/// # See also
///
/// * [`Components::component_id()`]
/// * [`Components::init_component()`]
pub fn init_component_with_descriptor(
/// * [`Components::register_component()`]
pub fn register_component_with_descriptor(
&mut self,
storages: &mut Storages,
descriptor: ComponentDescriptor,
) -> ComponentId {
Components::init_component_inner(&mut self.components, storages, descriptor)
Components::register_component_inner(&mut self.components, storages, descriptor)
}

#[inline]
fn init_component_inner(
fn register_component_inner(
components: &mut Vec<ComponentInfo>,
storages: &mut Storages,
descriptor: ComponentDescriptor,
Expand Down Expand Up @@ -966,7 +966,7 @@ impl Components {
/// instance.
///
/// Returns [`None`] if the `Component` type has not
/// yet been initialized using [`Components::init_component()`].
/// yet been initialized using [`Components::register_component()`].
///
/// ```
/// use bevy_ecs::prelude::*;
Expand All @@ -976,7 +976,7 @@ impl Components {
/// #[derive(Component)]
/// struct ComponentA;
///
/// let component_a_id = world.init_component::<ComponentA>();
/// let component_a_id = world.register_component::<ComponentA>();
///
/// assert_eq!(component_a_id, world.components().component_id::<ComponentA>().unwrap())
/// ```
Expand Down Expand Up @@ -1004,7 +1004,7 @@ impl Components {
/// instance.
///
/// Returns [`None`] if the `Resource` type has not
/// yet been initialized using [`Components::init_resource()`].
/// yet been initialized using [`Components::register_resource()`].
///
/// ```
/// use bevy_ecs::prelude::*;
Expand All @@ -1028,31 +1028,31 @@ impl Components {
self.get_resource_id(TypeId::of::<T>())
}

/// Initializes a [`Resource`] of type `T` with this instance.
/// If a resource of this type has already been initialized, this will return
/// Registers a [`Resource`] of type `T` with this instance.
/// If a resource of this type has already been registered, this will return
/// the ID of the pre-existing resource.
///
/// # See also
///
/// * [`Components::resource_id()`]
#[inline]
pub fn init_resource<T: Resource>(&mut self) -> ComponentId {
pub fn register_resource<T: Resource>(&mut self) -> ComponentId {
// SAFETY: The [`ComponentDescriptor`] matches the [`TypeId`]
unsafe {
self.get_or_insert_resource_with(TypeId::of::<T>(), || {
self.get_or_register_resource_with(TypeId::of::<T>(), || {
ComponentDescriptor::new_resource::<T>()
})
}
}

/// Initializes a [non-send resource](crate::system::NonSend) of type `T` with this instance.
/// If a resource of this type has already been initialized, this will return
/// Registers a [non-send resource](crate::system::NonSend) of type `T` with this instance.
/// If a resource of this type has already been registered, this will return
/// the ID of the pre-existing resource.
#[inline]
pub fn init_non_send<T: Any>(&mut self) -> ComponentId {
pub fn register_non_send<T: Any>(&mut self) -> ComponentId {
// SAFETY: The [`ComponentDescriptor`] matches the [`TypeId`]
unsafe {
self.get_or_insert_resource_with(TypeId::of::<T>(), || {
self.get_or_register_resource_with(TypeId::of::<T>(), || {
ComponentDescriptor::new_non_send::<T>(StorageType::default())
})
}
Expand All @@ -1062,7 +1062,7 @@ impl Components {
///
/// The [`ComponentDescriptor`] must match the [`TypeId`]
#[inline]
unsafe fn get_or_insert_resource_with(
unsafe fn get_or_register_resource_with(
&mut self,
type_id: TypeId,
func: impl FnOnce() -> ComponentDescriptor,
Expand Down Expand Up @@ -1293,7 +1293,7 @@ struct InitComponentId<T: Component> {
impl<T: Component> FromWorld for InitComponentId<T> {
fn from_world(world: &mut World) -> Self {
Self {
component_id: world.init_component::<T>(),
component_id: world.register_component::<T>(),
marker: PhantomData,
}
}
Expand Down Expand Up @@ -1384,7 +1384,7 @@ impl RequiredComponents {
storages: &mut Storages,
constructor: fn() -> C,
) {
let component_id = components.init_component::<C>(storages);
let component_id = components.register_component::<C>(storages);
let erased: RequiredComponentConstructor = RequiredComponentConstructor(Arc::new(
move |table,
sparse_sets,
Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ mod tests {
assert_eq!(
ids,
&[
world.init_component::<TableStored>(),
world.init_component::<SparseStored>(),
world.register_component::<TableStored>(),
world.register_component::<SparseStored>(),
]
);

Expand Down Expand Up @@ -235,10 +235,10 @@ mod tests {
assert_eq!(
ids,
&[
world.init_component::<A>(),
world.init_component::<TableStored>(),
world.init_component::<SparseStored>(),
world.init_component::<B>(),
world.register_component::<A>(),
world.register_component::<TableStored>(),
world.register_component::<SparseStored>(),
world.register_component::<B>(),
]
);

Expand Down Expand Up @@ -288,7 +288,7 @@ mod tests {
},
);

assert_eq!(ids, &[world.init_component::<C>(),]);
assert_eq!(ids, &[world.register_component::<C>(),]);

let e4 = world
.spawn(BundleWithIgnored {
Expand Down Expand Up @@ -2011,7 +2011,7 @@ mod tests {
struct Y;

let mut world = World::new();
let x_id = world.init_component::<X>();
let x_id = world.register_component::<X>();

let mut e = world.spawn_empty();

Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ mod tests {
world.flush();

let mut event = EventWithData { counter: 0 };
let component_a = world.init_component::<A>();
let component_a = world.register_component::<A>();
world.trigger_targets_ref(&mut event, component_a);
assert_eq!(5, event.counter);
}
Expand All @@ -756,7 +756,7 @@ mod tests {
fn observer_multiple_events() {
let mut world = World::new();
world.init_resource::<Order>();
let on_remove = world.init_component::<OnRemove>();
let on_remove = world.register_component::<OnRemove>();
world.spawn(
// SAFETY: OnAdd and OnRemove are both unit types, so this is safe
unsafe {
Expand All @@ -779,8 +779,8 @@ mod tests {
fn observer_multiple_components() {
let mut world = World::new();
world.init_resource::<Order>();
world.init_component::<A>();
world.init_component::<B>();
world.register_component::<A>();
world.register_component::<B>();

world.observe(|_: Trigger<OnAdd, (A, B)>, mut res: ResMut<Order>| res.observed("add_ab"));

Expand Down Expand Up @@ -883,7 +883,7 @@ mod tests {
let mut world = World::new();
world.init_resource::<Order>();

let component_id = world.init_component::<A>();
let component_id = world.register_component::<A>();
world.spawn(
Observer::new(|_: Trigger<OnAdd>, mut res: ResMut<Order>| res.observed("event_a"))
.with_component(component_id),
Expand All @@ -905,7 +905,7 @@ mod tests {
fn observer_dynamic_trigger() {
let mut world = World::new();
world.init_resource::<Order>();
let event_a = world.init_component::<EventA>();
let event_a = world.register_component::<EventA>();

world.spawn(ObserverState {
// SAFETY: we registered `event_a` above and it matches the type of TriggerA
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/observer/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ fn hook_on_add<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
_: ComponentId,
) {
world.commands().queue(move |world: &mut World| {
let event_type = world.init_component::<E>();
let event_type = world.register_component::<E>();
let mut components = Vec::new();
B::component_ids(&mut world.components, &mut world.storages, &mut |id| {
components.push(id);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/observer/trigger_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ pub struct TriggerEvent<E, Targets: TriggerTargets = ()> {

impl<E: Event, Targets: TriggerTargets> TriggerEvent<E, Targets> {
pub(super) fn trigger(mut self, world: &mut World) {
let event_type = world.init_component::<E>();
let event_type = world.register_component::<E>();
trigger_event(world, event_type, &mut self.event, self.targets);
}
}

impl<E: Event, Targets: TriggerTargets> TriggerEvent<&mut E, Targets> {
pub(super) fn trigger_ref(self, world: &mut World) {
let event_type = world.init_component::<E>();
let event_type = world.register_component::<E>();
trigger_event(world, event_type, self.event, self.targets);
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/query/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ mod tests {
let mut world = World::new();
let entity_a = world.spawn((A(0), B(0))).id();
let entity_b = world.spawn((A(0), C(0))).id();
let component_id_a = world.init_component::<A>();
let component_id_b = world.init_component::<B>();
let component_id_c = world.init_component::<C>();
let component_id_a = world.register_component::<A>();
let component_id_b = world.register_component::<B>();
let component_id_c = world.register_component::<C>();

let mut query_a = QueryBuilder::<Entity>::new(&mut world)
.with_id(component_id_a)
Expand Down Expand Up @@ -401,8 +401,8 @@ mod tests {
fn builder_dynamic_components() {
let mut world = World::new();
let entity = world.spawn((A(0), B(1))).id();
let component_id_a = world.init_component::<A>();
let component_id_b = world.init_component::<B>();
let component_id_a = world.register_component::<A>();
let component_id_b = world.register_component::<B>();

let mut query = QueryBuilder::<FilteredEntityRef>::new(&mut world)
.ref_id(component_id_a)
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ unsafe impl<T: Component> WorldQuery for &T {
}

fn init_state(world: &mut World) -> ComponentId {
world.init_component::<T>()
world.register_component::<T>()
}

fn get_state(components: &Components) -> Option<Self::State> {
Expand Down Expand Up @@ -1387,7 +1387,7 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> {
}

fn init_state(world: &mut World) -> ComponentId {
world.init_component::<T>()
world.register_component::<T>()
}

fn get_state(components: &Components) -> Option<Self::State> {
Expand Down Expand Up @@ -1586,7 +1586,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
}

fn init_state(world: &mut World) -> ComponentId {
world.init_component::<T>()
world.register_component::<T>()
}

fn get_state(components: &Components) -> Option<Self::State> {
Expand Down Expand Up @@ -1976,7 +1976,7 @@ unsafe impl<T: Component> WorldQuery for Has<T> {
}

fn init_state(world: &mut World) -> ComponentId {
world.init_component::<T>()
world.register_component::<T>()
}

fn get_state(components: &Components) -> Option<Self::State> {
Expand Down
Loading

0 comments on commit 35d1086

Please sign in to comment.