Skip to content

Commit

Permalink
Move entity functionality to separate module and clean up imports
Browse files Browse the repository at this point in the history
  • Loading branch information
LechintanTudor committed Oct 5, 2023
1 parent f8ce147 commit 98027b8
Show file tree
Hide file tree
Showing 30 changed files with 72 additions and 75 deletions.
1 change: 0 additions & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
comment_width = 100
force_multiline_blocks = true
group_imports = "One"
imports_granularity = "Module"
max_width = 100
newline_style = "Unix"
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/components/component_set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::{ComponentStorages, FamilyMask, GroupMask};
use crate::storage::{Component, ComponentStorage, Entity, EntityStorage};
use crate::components::{Component, ComponentStorage, ComponentStorages, FamilyMask, GroupMask};
use crate::entity::{Entity, EntityStorage};
use crate::utils::panic_missing_comp;
use std::any::TypeId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::Entity;
use crate::storage::{Component, DenseEntity, SparseArray};
use crate::components::Component;
use crate::entity::{DenseEntity, Entity, SparseArray};
use std::alloc::{alloc, dealloc, handle_alloc_error, realloc, Layout};
use std::ptr::NonNull;
use std::{mem, ptr, slice};
Expand All @@ -26,7 +26,7 @@ unsafe impl Send for ComponentStorage {}
unsafe impl Sync for ComponentStorage {}

impl ComponentStorage {
pub(crate) fn new<T>() -> Self
pub fn new<T>() -> Self
where
T: Component,
{
Expand All @@ -46,7 +46,7 @@ impl ComponentStorage {
}
}

pub(crate) unsafe fn insert<T>(&mut self, entity: Entity, component: T) -> Option<T>
pub unsafe fn insert<T>(&mut self, entity: Entity, component: T) -> Option<T>
where
T: Component,
{
Expand Down Expand Up @@ -74,7 +74,7 @@ impl ComponentStorage {
}
}

pub(crate) unsafe fn remove<T>(&mut self, entity: Entity) -> Option<T>
pub unsafe fn remove<T>(&mut self, entity: Entity) -> Option<T>
where
T: Component,
{
Expand All @@ -97,11 +97,11 @@ impl ComponentStorage {
}

#[inline]
pub(crate) fn delete(&mut self, entity: Entity) {
pub fn delete(&mut self, entity: Entity) {
unsafe { (self.fns.delete)(self, entity) }
}

pub(crate) unsafe fn delete_typed<T>(&mut self, entity: Entity)
pub unsafe fn delete_typed<T>(&mut self, entity: Entity)
where
T: Component,
{
Expand All @@ -127,7 +127,7 @@ impl ComponentStorage {
}

#[inline]
pub(crate) unsafe fn swap_nonoverlapping(&mut self, a: usize, b: usize) {
pub unsafe fn swap_nonoverlapping(&mut self, a: usize, b: usize) {
(self.fns.swap_nonoverlapping)(self, a, b);
}

Expand Down Expand Up @@ -156,7 +156,7 @@ impl ComponentStorage {
}

#[inline]
pub(crate) unsafe fn get<T>(&self, entity: Entity) -> Option<&T>
pub unsafe fn get<T>(&self, entity: Entity) -> Option<&T>
where
T: Component,
{
Expand All @@ -165,7 +165,7 @@ impl ComponentStorage {
}

#[inline]
pub(crate) unsafe fn get_mut<T>(&mut self, entity: Entity) -> Option<&mut T>
pub unsafe fn get_mut<T>(&mut self, entity: Entity) -> Option<&mut T>
where
T: Component,
{
Expand All @@ -174,32 +174,32 @@ impl ComponentStorage {
}

#[inline]
pub(crate) fn len(&self) -> usize {
pub fn len(&self) -> usize {
self.len
}

#[inline]
pub(crate) fn is_empty(&self) -> bool {
pub fn is_empty(&self) -> bool {
self.len == 0
}

#[inline]
pub(crate) fn entities(&self) -> &[Entity] {
pub fn entities(&self) -> &[Entity] {
unsafe { slice::from_raw_parts(self.entities.as_ptr(), self.len) }
}

#[inline]
pub(crate) unsafe fn components<T>(&self) -> &[T] {
pub unsafe fn components<T>(&self) -> &[T] {
slice::from_raw_parts(self.components.cast::<T>().as_ptr(), self.len)
}

#[inline]
pub(crate) unsafe fn components_mut<T>(&mut self) -> &mut [T] {
pub unsafe fn components_mut<T>(&mut self) -> &mut [T] {
slice::from_raw_parts_mut(self.components.cast::<T>().as_ptr(), self.len)
}

#[inline]
pub(crate) unsafe fn split<T>(&self) -> (&[Entity], &SparseArray, &[T]) {
pub unsafe fn split<T>(&self) -> (&[Entity], &SparseArray, &[T]) {
(
slice::from_raw_parts(self.entities.as_ptr(), self.len),
&self.sparse,
Expand All @@ -208,7 +208,7 @@ impl ComponentStorage {
}

#[inline]
pub(crate) unsafe fn split_mut<T>(&mut self) -> (&[Entity], &SparseArray, &mut [T]) {
pub unsafe fn split_mut<T>(&mut self) -> (&[Entity], &SparseArray, &mut [T]) {
(
slice::from_raw_parts(self.entities.as_ptr(), self.len),
&self.sparse,
Expand All @@ -217,7 +217,7 @@ impl ComponentStorage {
}

#[inline]
pub(crate) fn clear(&mut self) {
pub fn clear(&mut self) {
unsafe { (self.fns.clear)(self) }
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::{ComponentStorages, FamilyMask, GroupMask, QueryMask};
use crate::storage::{ComponentStorage, Entity};
use crate::components::{ComponentStorage, ComponentStorages, FamilyMask, GroupMask, QueryMask};
use crate::entity::Entity;
use atomic_refcell::AtomicRefCell;
use std::ops::Range;

Expand Down
10 changes: 7 additions & 3 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
//! Manages component storages and handles component grouping.

mod component;
mod component_set;
mod component_storage;
mod group;
mod group_info;
mod group_mask;

pub use self::component::*;
pub use self::component_set::*;
pub(crate) use self::group::*;
pub use self::group_info::*;

pub(crate) use self::component_storage::*;
pub(crate) use self::group::*;
pub(crate) use self::group_mask::*;

use crate::layout::Layout;
pub use crate::storage::Component;
use crate::storage::ComponentStorage;
use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use rustc_hash::FxHashMap;
use std::any::TypeId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::Entity;
use crate::entity::Entity;
use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::storage::{DenseEntity, Entity, SparseArray};
use crate::entity::{DenseEntity, Entity, SparseArray};
use std::convert::AsRef;

/// Sparse set-based storage for entities.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::storage::{Entity, EntityAllocator, EntitySparseSet};
use crate::entity::{Entity, EntityAllocator, EntitySparseSet};

/// Sparse set-based storage for entities.
#[derive(Default, Debug)]
Expand Down
12 changes: 12 additions & 0 deletions src/storage/entity.rs → src/entity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
//! Manages entity allocation and storage.

mod entity_allocator;
mod entity_sparse_set;
mod entity_storage;
mod sparse_array;

pub use self::entity_allocator::*;
pub use self::entity_sparse_set::*;
pub use self::entity_storage::*;
pub use self::sparse_array::*;

use std::cmp::{Ord, Ordering, PartialOrd};
use std::fmt;
use std::num::NonZeroU32;
Expand Down
2 changes: 1 addition & 1 deletion src/storage/sparse_array.rs → src/entity/sparse_array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::storage::{DenseEntity, Entity};
use crate::entity::{DenseEntity, Entity};

/// Maps versioned sparse indexes (entities) to dense indexes.
/// Used internally by `ComponentStorage`.
Expand Down
2 changes: 1 addition & 1 deletion src/layout/group.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::storage::Component;
use crate::components::Component;
use crate::utils::ComponentData;

/// The minimum number of component storages that can form a group.
Expand Down
1 change: 1 addition & 0 deletions src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod group;
mod group_family;

pub use self::group::*;

pub(crate) use self::group_family::*;

/// The maximum number of groups in a [`Layout`].
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@
//! ```

pub mod components;
pub mod entity;
pub mod layout;
pub mod query;
pub mod resources;
pub mod storage;
pub mod systems;
pub mod utils;
pub mod world;

/// Re-exports the most commonly used items.
pub mod prelude {
pub use crate::entity::Entity;
pub use crate::layout::{Layout, LayoutGroupDescriptor};
pub use crate::query::{
BuildCompoundQuery, ImmutableComponentView, IntoEntityIter, MutableComponentView, Query,
};
pub use crate::resources::{Res, ResMut, Resources};
pub use crate::storage::Entity;
pub use crate::systems::{IntoExclusiveSystem, IntoLocalSystem, IntoSystem, Schedule};
pub use crate::world::{Comp, CompMut, Entities, World};
}
Expand Down
4 changes: 2 additions & 2 deletions src/query/component_view.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::GroupInfo;
use crate::storage::{Component, Entity, SparseArray};
use crate::components::{Component, GroupInfo};
use crate::entity::{Entity, SparseArray};
use std::ops::RangeBounds;

/// Trait representing an immutable component view that can be a part of a query.
Expand Down
2 changes: 1 addition & 1 deletion src/query/iter/dense_iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::entity::Entity;
use crate::query::{EntityIterator, QueryPart};
use crate::storage::Entity;

/// Iterator over grouped storages. Extremely fast.
pub struct DenseIter<'a, G>
Expand Down
2 changes: 1 addition & 1 deletion src/query/iter/entities_iter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::storage::Entity;
use crate::entity::Entity;

/// Trait representing component iterators that are able to return the entity to which the
/// components belong.
Expand Down
3 changes: 2 additions & 1 deletion src/query/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ mod sparse_iter;
pub use self::dense_iter::*;
pub use self::entities_iter::*;
pub use self::sparse_iter::*;

use crate::entity::Entity;
use crate::query::iter::{DenseIter, SparseIter};
use crate::query::{self, QueryPart};
use crate::storage::Entity;

/// Iterator over grouped or ungrouped storages.
pub enum Iter<'a, G, I, E>
Expand Down
2 changes: 1 addition & 1 deletion src/query/iter/sparse_iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::entity::Entity;
use crate::query::{EntityIterator, QueryPart};
use crate::storage::Entity;
use std::slice::Iter as SliceIter;

/// Iterator over ungrouped storages.
Expand Down
3 changes: 2 additions & 1 deletion src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub use self::into_query_parts::*;
pub use self::iter::*;
pub use self::query_group_info::*;
pub use self::query_part::*;
use crate::storage::Entity;

use crate::entity::Entity;

/// Allows fetching and iterating entities and components in
/// [`BorrowedComponentViews`](crate::query::BorrowedComponentView).
Expand Down
2 changes: 1 addition & 1 deletion src/query/query_part.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::entity::{Entity, SparseArray};
use crate::query::{BorrowedComponentView, QueryGroupInfo};
use crate::storage::{Entity, SparseArray};
use std::ops::RangeBounds;

/// Allows getting, including or excluding components in a query.
Expand Down
2 changes: 2 additions & 0 deletions src/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ mod unsafe_resources;
pub use self::res::*;
pub use self::resource::*;
pub use self::sync_resources::*;

pub(crate) use self::unsafe_resources::*;

use std::any::TypeId;
use std::fmt;
use std::marker::PhantomData;
Expand Down
17 changes: 0 additions & 17 deletions src/storage/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/systems/system_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::Component;
use crate::resources::{Res, ResMut, Resource, Resources, SyncResources};
use crate::storage::Component;
use crate::systems::SystemDataDescriptor;
use crate::world::{Comp, CompMut, Entities, World};

Expand Down
2 changes: 1 addition & 1 deletion src/systems/system_data_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::Component;
use crate::resources::{Res, ResMut, Resource};
use crate::storage::Component;
use crate::systems::SystemDataType;
use crate::utils::{ComponentData, ResourceData};
use crate::world::{Comp, CompMut, Entities};
Expand Down
3 changes: 2 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod impl_macros;
mod panic;
mod type_data;

pub use self::type_data::*;

pub(crate) use self::impl_macros::*;
pub(crate) use self::panic::*;
pub use self::type_data::*;
2 changes: 1 addition & 1 deletion src/utils/type_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::{Component, ComponentStorage};
use crate::resources::Resource;
use crate::storage::{Component, ComponentStorage};
use std::any::{self, TypeId};
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use std::fmt;
Expand Down
Loading

0 comments on commit 98027b8

Please sign in to comment.