Skip to content

Commit db6db96

Browse files
committed
Fold TypeInfo into ComponentDescriptor
1 parent 721071d commit db6db96

File tree

2 files changed

+36
-103
lines changed

2 files changed

+36
-103
lines changed

crates/bevy_ecs/src/component/mod.rs renamed to crates/bevy_ecs/src/component.rs

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
mod type_info;
2-
3-
pub use type_info::*;
4-
51
use crate::storage::SparseSetIndex;
62
use std::{
73
alloc::Layout,
@@ -140,32 +136,30 @@ pub struct ComponentDescriptor {
140136
}
141137

142138
impl ComponentDescriptor {
139+
// SAFETY: The pointer points to a valid value of type `T` and it is safe to drop this value.
140+
unsafe fn drop_ptr<T>(x: *mut u8) {
141+
x.cast::<T>().drop_in_place()
142+
}
143+
143144
pub fn new<T: Component>(storage_type: StorageType) -> Self {
144145
Self {
145146
name: std::any::type_name::<T>().to_string(),
146147
storage_type,
147148
is_send_and_sync: true,
148149
type_id: Some(TypeId::of::<T>()),
149150
layout: Layout::new::<T>(),
150-
drop: TypeInfo::drop_ptr::<T>,
151+
drop: Self::drop_ptr::<T>,
151152
}
152153
}
153154

154-
/// # Safety
155-
///
156-
/// The [`TypeInfo`] must match the [`TypeId`]
157-
pub unsafe fn from_type_info(
158-
storage_type: StorageType,
159-
type_id: Option<TypeId>,
160-
type_info: TypeInfo,
161-
) -> Self {
155+
fn new_non_send<T: Any>(storage_type: StorageType) -> Self {
162156
Self {
163-
name: type_info.type_name().to_string(),
157+
name: std::any::type_name::<T>().to_string(),
164158
storage_type,
165-
is_send_and_sync: type_info.is_send_and_sync(),
166-
type_id,
167-
layout: type_info.layout(),
168-
drop: type_info.drop(),
159+
is_send_and_sync: false,
160+
type_id: Some(TypeId::of::<T>()),
161+
layout: Layout::new::<T>(),
162+
drop: Self::drop_ptr::<T>,
169163
}
170164
}
171165

@@ -222,8 +216,12 @@ impl Components {
222216

223217
#[inline]
224218
pub fn get_or_insert_id<T: Component>(&mut self) -> ComponentId {
225-
// SAFE: The [`TypeInfo`] matches the [`TypeId`]
226-
unsafe { self.get_or_insert_with(TypeId::of::<T>(), TypeInfo::of::<T>) }
219+
// SAFE: The [`ComponentDescriptor`] matches the [`TypeId`]
220+
unsafe {
221+
self.get_or_insert_with(TypeId::of::<T>(), || {
222+
ComponentDescriptor::new::<T>(StorageType::default())
223+
})
224+
}
227225
}
228226

229227
#[inline]
@@ -271,39 +269,38 @@ impl Components {
271269

272270
#[inline]
273271
pub fn get_or_insert_resource_id<T: Component>(&mut self) -> ComponentId {
274-
// SAFE: The [`TypeInfo`] matches the [`TypeId`]
275-
unsafe { self.get_or_insert_resource_with(TypeId::of::<T>(), TypeInfo::of::<T>) }
272+
// SAFE: The [`ComponentDescriptor`] matches the [`TypeId`]
273+
unsafe {
274+
self.get_or_insert_resource_with(TypeId::of::<T>(), || {
275+
ComponentDescriptor::new::<T>(StorageType::default())
276+
})
277+
}
276278
}
277279

278280
#[inline]
279281
pub fn get_or_insert_non_send_resource_id<T: Any>(&mut self) -> ComponentId {
280-
// SAFE: The [`TypeInfo`] matches the [`TypeId`]
282+
// SAFE: The [`ComponentDescriptor`] matches the [`TypeId`]
281283
unsafe {
282-
self.get_or_insert_resource_with(TypeId::of::<T>(), TypeInfo::of_non_send_and_sync::<T>)
284+
self.get_or_insert_resource_with(TypeId::of::<T>(), || {
285+
ComponentDescriptor::new_non_send::<T>(StorageType::default())
286+
})
283287
}
284288
}
285289

286290
/// # Safety
287291
///
288-
/// The [`TypeInfo`] must match the [`TypeId`]
292+
/// The [`ComponentDescriptor`] must match the [`TypeId`]
289293
#[inline]
290294
unsafe fn get_or_insert_resource_with(
291295
&mut self,
292296
type_id: TypeId,
293-
func: impl FnOnce() -> TypeInfo,
297+
func: impl FnOnce() -> ComponentDescriptor,
294298
) -> ComponentId {
295299
let components = &mut self.components;
296300
let index = self.resource_indices.entry(type_id).or_insert_with(|| {
297-
let type_info = func();
301+
let descriptor = func();
298302
let index = components.len();
299-
components.push(ComponentInfo::new(
300-
ComponentId(index),
301-
ComponentDescriptor::from_type_info(
302-
StorageType::default(),
303-
Some(type_id),
304-
type_info,
305-
),
306-
));
303+
components.push(ComponentInfo::new(ComponentId(index), descriptor));
307304
index
308305
});
309306

@@ -312,25 +309,18 @@ impl Components {
312309

313310
/// # Safety
314311
///
315-
/// The [`TypeInfo`] must match the [`TypeId`]
312+
/// The [`ComponentDescriptor`] must match the [`TypeId`]
316313
#[inline]
317314
pub(crate) unsafe fn get_or_insert_with(
318315
&mut self,
319316
type_id: TypeId,
320-
func: impl FnOnce() -> TypeInfo,
317+
func: impl FnOnce() -> ComponentDescriptor,
321318
) -> ComponentId {
322319
let components = &mut self.components;
323320
let index = self.indices.entry(type_id).or_insert_with(|| {
324-
let type_info = func();
321+
let descriptor = func();
325322
let index = components.len();
326-
components.push(ComponentInfo::new(
327-
ComponentId(index),
328-
ComponentDescriptor::from_type_info(
329-
StorageType::default(),
330-
Some(type_id),
331-
type_info,
332-
),
333-
));
323+
components.push(ComponentInfo::new(ComponentId(index), descriptor));
334324
index
335325
});
336326

crates/bevy_ecs/src/component/type_info.rs

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)