Skip to content

Commit 367b630

Browse files
move methods to World (except take_component)
1 parent 579d053 commit 367b630

File tree

3 files changed

+224
-290
lines changed

3 files changed

+224
-290
lines changed

crates/bevy_ecs/src/storage/mod.rs

Lines changed: 0 additions & 238 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ pub use resource::*;
99
pub use sparse_set::*;
1010
pub use table::*;
1111

12-
use crate::{
13-
archetype::Archetypes,
14-
component::{ComponentId, ComponentTicks, Components, StorageType, TickCells},
15-
entity::{Entity, EntityLocation},
16-
};
17-
use bevy_ptr::{OwningPtr, Ptr};
18-
use std::any::TypeId;
19-
2012
/// The raw data stores of a [World](crate::world::World)
2113
#[derive(Default)]
2214
pub struct Storages {
@@ -25,233 +17,3 @@ pub struct Storages {
2517
pub resources: Resources<true>,
2618
pub non_send_resources: Resources<false>,
2719
}
28-
29-
impl Storages {
30-
/// Get a raw pointer to a particular [`Component`](crate::component::Component) and its [`ComponentTicks`] identified by their [`TypeId`]
31-
///
32-
/// # Safety
33-
/// - `storage_type` must accurately reflect where the components for `component_id` are stored.
34-
/// - `location` must refer to an archetype that contains `entity`
35-
/// - `Archetypes` and `Components` must come from the world this of this `Storages`
36-
/// - the caller must ensure that no aliasing rules are violated
37-
#[inline]
38-
pub(crate) unsafe fn get_component_and_ticks_with_type(
39-
&self,
40-
archetypes: &Archetypes,
41-
components: &Components,
42-
type_id: TypeId,
43-
storage_type: StorageType,
44-
entity: Entity,
45-
location: EntityLocation,
46-
) -> Option<(Ptr<'_>, TickCells<'_>)> {
47-
let component_id = components.get_id(type_id)?;
48-
// SAFETY: component_id is valid, the rest is deferred to caller
49-
self.get_component_and_ticks(archetypes, component_id, storage_type, entity, location)
50-
}
51-
52-
/// Get a raw pointer to a particular [`Component`](crate::component::Component) and its [`ComponentTicks`]
53-
///
54-
/// # Safety
55-
/// - `location` must refer to an archetype that contains `entity`
56-
/// - `component_id` must be valid
57-
/// - `storage_type` must accurately reflect where the components for `component_id` are stored.
58-
/// - `Archetypes` and `Components` must come from the world this of this `Storages`
59-
/// - the caller must ensure that no aliasing rules are violated
60-
#[inline]
61-
pub(crate) unsafe fn get_component_and_ticks(
62-
&self,
63-
archetypes: &Archetypes,
64-
component_id: ComponentId,
65-
storage_type: StorageType,
66-
entity: Entity,
67-
location: EntityLocation,
68-
) -> Option<(Ptr<'_>, TickCells<'_>)> {
69-
match storage_type {
70-
StorageType::Table => {
71-
let (components, table_row) =
72-
fetch_table(archetypes, self, location, component_id)?;
73-
74-
// SAFETY: archetypes only store valid table_rows and caller ensure aliasing rules
75-
Some((
76-
components.get_data_unchecked(table_row),
77-
TickCells {
78-
added: components.get_added_ticks_unchecked(table_row),
79-
changed: components.get_changed_ticks_unchecked(table_row),
80-
},
81-
))
82-
}
83-
StorageType::SparseSet => fetch_sparse_set(self, component_id)?.get_with_ticks(entity),
84-
}
85-
}
86-
87-
/// Get a raw pointer to a particular [`Component`](crate::component::Component) on a particular [`Entity`], identified by the component's type
88-
///
89-
/// # Safety
90-
/// - `location` must refer to an archetype that contains `entity`
91-
/// the archetype
92-
/// - `storage_type` must accurately reflect where the components for `component_id` are stored.
93-
/// - `Archetypes` and `Components` must come from the world this of this `Storages`
94-
/// - the caller must ensure that no aliasing rules are violated
95-
#[inline]
96-
pub(crate) unsafe fn get_component_with_type(
97-
&self,
98-
archetypes: &Archetypes,
99-
components: &Components,
100-
type_id: TypeId,
101-
storage_type: StorageType,
102-
entity: Entity,
103-
location: EntityLocation,
104-
) -> Option<Ptr<'_>> {
105-
let component_id = components.get_id(type_id)?;
106-
// SAFETY: component_id is valid, the rest is deferred to caller
107-
self.get_component(archetypes, component_id, storage_type, entity, location)
108-
}
109-
110-
/// Get a raw pointer to a particular [`Component`](crate::component::Component) on a particular [`Entity`] in the provided [`World`](crate::world::World).
111-
///
112-
/// # Safety
113-
/// - `location` must refer to an archetype that contains `entity`
114-
/// the archetype
115-
/// - `component_id`
116-
/// - `storage_type` must accurately reflect where the components for `component_id` are stored.
117-
/// - `Archetypes` and `Components` must come from the world this of this `Storages`
118-
/// - the caller must ensure that no aliasing rules are violated
119-
#[inline]
120-
pub(crate) unsafe fn get_component(
121-
&self,
122-
archetypes: &Archetypes,
123-
component_id: ComponentId,
124-
storage_type: StorageType,
125-
entity: Entity,
126-
location: EntityLocation,
127-
) -> Option<Ptr<'_>> {
128-
// SAFETY: component_id exists and is therefore valid
129-
match storage_type {
130-
StorageType::Table => {
131-
let (components, table_row) =
132-
fetch_table(archetypes, self, location, component_id)?;
133-
// SAFETY: archetypes only store valid table_rows and caller ensure aliasing rules
134-
Some(components.get_data_unchecked(table_row))
135-
}
136-
StorageType::SparseSet => fetch_sparse_set(self, component_id)?.get(entity),
137-
}
138-
}
139-
140-
/// Get a raw pointer to the [`ComponentTicks`] on a particular [`Entity`], identified by the component's [`TypeId`]
141-
///
142-
/// # Safety
143-
/// - `location` must refer to an archetype that contains `entity`
144-
/// the archetype
145-
/// - `storage_type` must accurately reflect where the components for `component_id` are stored.
146-
/// - `Archetypes` and `Components` must come from the world this of this `Storages`
147-
/// - the caller must ensure that no aliasing rules are violated
148-
#[inline]
149-
pub(crate) unsafe fn get_ticks_with_type(
150-
&self,
151-
archetypes: &Archetypes,
152-
components: &Components,
153-
type_id: TypeId,
154-
storage_type: StorageType,
155-
entity: Entity,
156-
location: EntityLocation,
157-
) -> Option<ComponentTicks> {
158-
let component_id = components.get_id(type_id)?;
159-
// SAFETY: component_id is valid, the rest is deferred to caller
160-
self.get_ticks(archetypes, component_id, storage_type, entity, location)
161-
}
162-
163-
/// Get a raw pointer to the [`ComponentTicks`] on a particular [`Entity`]
164-
///
165-
/// # Safety
166-
/// - `location` must refer to an archetype that contains `entity`
167-
/// the archetype
168-
/// - `component_id` must be valid
169-
/// - `storage_type` must accurately reflect where the components for `component_id` are stored.
170-
/// - `Archetypes` and `Components` must come from the world this of this `Storages`
171-
/// - the caller must ensure that no aliasing rules are violated
172-
#[inline]
173-
pub(crate) unsafe fn get_ticks(
174-
&self,
175-
archetypes: &Archetypes,
176-
component_id: ComponentId,
177-
storage_type: StorageType,
178-
entity: Entity,
179-
location: EntityLocation,
180-
) -> Option<ComponentTicks> {
181-
match storage_type {
182-
StorageType::Table => {
183-
let (components, table_row) =
184-
fetch_table(archetypes, self, location, component_id)?;
185-
// SAFETY: archetypes only store valid table_rows and caller ensure aliasing rules
186-
Some(components.get_ticks_unchecked(table_row))
187-
}
188-
StorageType::SparseSet => fetch_sparse_set(self, component_id)?.get_ticks(entity),
189-
}
190-
}
191-
}
192-
193-
impl Storages {
194-
/// Moves component data out of storage.
195-
///
196-
/// This function leaves the underlying memory unchanged, but the component behind
197-
/// returned pointer is semantically owned by the caller and will not be dropped in its original location.
198-
/// Caller is responsible to drop component data behind returned pointer.
199-
///
200-
/// # Safety
201-
/// - `location` must be within bounds of the given archetype and `entity` must exist inside the `archetype`
202-
/// - `component_id` must be valid
203-
/// - `components` must come from the same world as `self`
204-
/// - The relevant table row **must be removed** by the caller once all components are taken, without dropping the value
205-
#[inline]
206-
pub(crate) unsafe fn take_component<'a>(
207-
&'a mut self,
208-
components: &Components,
209-
removed_components: &mut SparseSet<ComponentId, Vec<Entity>>,
210-
component_id: ComponentId,
211-
entity: Entity,
212-
location: EntityLocation,
213-
) -> OwningPtr<'a> {
214-
let component_info = components.get_info_unchecked(component_id);
215-
let removed_components = removed_components.get_or_insert_with(component_id, Vec::new);
216-
removed_components.push(entity);
217-
match component_info.storage_type() {
218-
StorageType::Table => {
219-
let table = &mut self.tables[location.table_id];
220-
// SAFETY: archetypes will always point to valid columns
221-
let components = table.get_column_mut(component_id).unwrap();
222-
// SAFETY:
223-
// - archetypes only store valid table_rows
224-
// - index is in bounds as promised by caller
225-
// - promote is safe because the caller promises to remove the table row without dropping it immediately afterwards
226-
components
227-
.get_data_unchecked_mut(location.table_row)
228-
.promote()
229-
}
230-
StorageType::SparseSet => self
231-
.sparse_sets
232-
.get_mut(component_id)
233-
.unwrap()
234-
.remove_and_forget(entity)
235-
.unwrap(),
236-
}
237-
}
238-
}
239-
240-
#[inline]
241-
unsafe fn fetch_table<'s>(
242-
archetypes: &Archetypes,
243-
storages: &'s Storages,
244-
location: EntityLocation,
245-
component_id: ComponentId,
246-
) -> Option<(&'s Column, TableRow)> {
247-
let archetype = &archetypes[location.archetype_id];
248-
let table = &storages.tables[archetype.table_id()];
249-
let components = table.get_column(component_id)?;
250-
let table_row = archetype.entity_table_row(location.archetype_row);
251-
Some((components, table_row))
252-
}
253-
254-
#[inline]
255-
fn fetch_sparse_set(storages: &Storages, component_id: ComponentId) -> Option<&ComponentSparseSet> {
256-
storages.sparse_sets.get(component_id)
257-
}

0 commit comments

Comments
 (0)