use double ended iterators for component stuffs for testing purposes

This commit is contained in:
bgkillas 2025-08-16 18:37:45 -04:00
parent fe386ac2ff
commit 49f6999bc4
8 changed files with 108 additions and 46 deletions

View file

@ -2169,11 +2169,11 @@ impl RemoteDiffModel {
}
}
/*pub(crate) fn drain_backtrack(&mut self) -> impl Iterator<Item = EntityID> + '_ {
/*pub(crate) fn drain_backtrack(&mut self) -> impl DoubleEndedIterator<Item = EntityID> + '_ {
self.backtrack.drain(..)
}*/
pub(crate) fn drain_grab_request(&mut self) -> impl Iterator<Item = Lid> + '_ {
pub(crate) fn drain_grab_request(&mut self) -> impl DoubleEndedIterator<Item = Lid> + '_ {
self.grab_request.drain(..)
}
}

View file

@ -51,7 +51,7 @@ impl InterestTracker {
std::mem::take(&mut self.added_any)
}
pub(crate) fn drain_lost_interest(&mut self) -> impl Iterator<Item = PeerId> + '_ {
pub(crate) fn drain_lost_interest(&mut self) -> impl DoubleEndedIterator<Item = PeerId> + '_ {
self.lost_interest.drain(..)
}

View file

@ -343,7 +343,7 @@ impl EntityID {
isize::from(self.0)
}
pub fn children(self, tag: Option<Cow<'_, str>>) -> impl Iterator<Item = EntityID> {
pub fn children(self, tag: Option<Cow<'_, str>>) -> impl DoubleEndedIterator<Item = EntityID> {
raw::entity_get_all_children(self, tag)
.unwrap_or(None)
.unwrap_or_default()
@ -1566,7 +1566,7 @@ impl EntityManager {
pub fn iter_all_components_of_type<C: Component>(
&self,
tag: ComponentTag,
) -> impl Iterator<Item = C> {
) -> impl DoubleEndedIterator<Item = C> {
if self.use_cache {
return self
.entity()
@ -1588,7 +1588,7 @@ impl EntityManager {
}
fn iter_mut_all_components_of_type<C: Component>(
&mut self,
) -> impl Iterator<Item = &mut ComponentData> {
) -> impl DoubleEndedIterator<Item = &mut ComponentData> {
self.current_data.components[const { CachedComponent::from_component::<C>() as usize }]
.iter_mut()
.filter(|c| c.enabled)
@ -1596,7 +1596,7 @@ impl EntityManager {
pub fn iter_all_components_of_type_including_disabled<C: Component>(
&self,
tag: ComponentTag,
) -> impl Iterator<Item = C> {
) -> impl DoubleEndedIterator<Item = C> {
if self.use_cache {
return self
.entity()
@ -1620,7 +1620,7 @@ impl EntityManager {
}
fn iter_all_components_of_type_including_disabled_raw<C: Component>(
&self,
) -> impl Iterator<Item = &ComponentData> {
) -> impl DoubleEndedIterator<Item = &ComponentData> {
self.current_data.components[const { CachedComponent::from_component::<C>() as usize }]
.iter()
}

View file

@ -52,7 +52,11 @@ impl LuaState {
unsafe { LUA.lua_tointeger(self.lua, index) }
}
pub fn to_integer_array(&self, index: i32, len: usize) -> impl Iterator<Item = isize> {
pub fn to_integer_array(
&self,
index: i32,
len: usize,
) -> impl DoubleEndedIterator<Item = isize> {
(1..=len).map(move |i| unsafe {
LUA.lua_pushinteger(self.lua, i as lua_bindings::lua_Integer);
LUA.lua_gettable(self.lua, index);

View file

@ -225,6 +225,12 @@ impl<T: Debug> Debug for StdVec<T> {
f.debug_tuple("StdVec").field(&self.as_ref()).finish()
}
}
impl<T> Default for StdVec<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> StdVec<T> {
pub fn null() -> Self {
Self {
@ -294,6 +300,14 @@ impl<T> StdVec<T> {
self.cap = unsafe { new_ptr.add(new_cap) };
}
}
pub fn with_capacity(n: usize) -> StdVec<T> {
let mut v = Self::null();
v.alloc(n);
v
}
pub fn new() -> StdVec<T> {
Self::with_capacity(1)
}
pub fn push(&mut self, value: T) {
self.alloc(1);
unsafe {

View file

@ -71,6 +71,19 @@ impl ComponentTypeManager {
unsafe { mgr.as_mut() }.unwrap()
}
}
#[test]
fn test_com_create() {
let com_buffer = ComponentBuffer {
vtable: &ComponentManagerVTable {},
end: usize::MAX,
unk: [0, 0],
entity_entry: StdVec::new(),
entities: StdVec::new(),
prev: StdVec::new(),
next: StdVec::new(),
component_list: StdVec::new(),
};
}
#[repr(C)]
#[derive(Debug)]
pub struct ComponentBuffer {
@ -90,6 +103,7 @@ impl ComponentBuffer {
id: usize,
type_id: usize,
) -> &'static mut C {
crate::print!("{id} {type_id} {}", entity.entry);
let com = C::default(ComponentData {
vtable: self
.component_list
@ -114,6 +128,14 @@ impl ComponentBuffer {
});
let com = Box::leak(Box::new(com));
let index = self.component_list.len();
crate::print!(
"{index} {} {} {} {} {:?}",
self.component_list.len(),
self.entities.len(),
self.next.len(),
self.prev.len(),
self.next
);
self.component_list.push((com as *mut C).cast());
if self.entities.len() > index {
self.entities[index] = entity;
@ -123,6 +145,7 @@ impl ComponentBuffer {
}
self.entities.push(entity);
}
crate::print!("a");
while self.entity_entry.len() <= entity.entry {
self.entity_entry.push(self.end)
}
@ -131,23 +154,33 @@ impl ComponentBuffer {
&& e != self.end
{
off = e;
crate::print!("b");
while let Some(next) = self.next.get(off).copied()
&& next != self.end
{
off = next;
}
crate::print!("c");
while self.next.len() <= index {
self.next.push(self.end)
}
self.next[off] = index;
} else {
off = self.next.len();
off = index;
self.entity_entry[entity.entry] = off;
crate::print!("c");
while self.next.len() <= index {
self.next.push(self.end)
}
self.next[off] = self.end;
}
while self.next.len() <= off {
self.next.push(self.end)
}
crate::print!("d");
while self.prev.len() <= index {
self.prev.push(self.end)
}
self.next[off] = index;
crate::print!("{off}");
self.prev[index] = off;
crate::print!("e");
unsafe { std::mem::transmute(self.component_list.last_mut().unwrap()) }
}
pub fn iter_components(&self, entry: usize) -> ComponentIter {
@ -188,13 +221,15 @@ impl ComponentBuffer {
}
}
}
pub fn iter_every_component(&self) -> impl Iterator<Item = &'static ComponentData> {
pub fn iter_every_component(&self) -> impl DoubleEndedIterator<Item = &'static ComponentData> {
self.component_list
.as_ref()
.iter()
.filter_map(|c| unsafe { c.as_ref() })
}
pub fn iter_every_component_mut(&mut self) -> impl Iterator<Item = &'static mut ComponentData> {
pub fn iter_every_component_mut(
&mut self,
) -> impl DoubleEndedIterator<Item = &'static mut ComponentData> {
self.component_list
.as_mut()
.iter_mut()
@ -205,7 +240,7 @@ impl ComponentBuffer {
tag_manager: &TagManager<u8>,
entry: usize,
tag: &StdString,
) -> impl Iterator<Item = &'static ComponentData> {
) -> impl DoubleEndedIterator<Item = &'static ComponentData> {
self.iter_components(entry)
.filter(|c| c.tags.has_tag(tag_manager, tag))
}
@ -214,32 +249,32 @@ impl ComponentBuffer {
tag_manager: &TagManager<u8>,
entry: usize,
tag: &StdString,
) -> impl Iterator<Item = &'static mut ComponentData> {
) -> impl DoubleEndedIterator<Item = &'static mut ComponentData> {
self.iter_components_mut(entry)
.filter(|c| c.tags.has_tag(tag_manager, tag))
}
pub fn iter_enabled_components(
&self,
entry: usize,
) -> impl Iterator<Item = &'static ComponentData> {
) -> impl DoubleEndedIterator<Item = &'static ComponentData> {
self.iter_components(entry).filter(|c| c.enabled)
}
pub fn iter_disabled_components(
&self,
entry: usize,
) -> impl Iterator<Item = &'static ComponentData> {
) -> impl DoubleEndedIterator<Item = &'static ComponentData> {
self.iter_components(entry).filter(|c| !c.enabled)
}
pub fn iter_enabled_components_mut(
&mut self,
entry: usize,
) -> impl Iterator<Item = &'static mut ComponentData> {
) -> impl DoubleEndedIterator<Item = &'static mut ComponentData> {
self.iter_components_mut(entry).filter(|c| c.enabled)
}
pub fn iter_disabled_components_mut(
&mut self,
entry: usize,
) -> impl Iterator<Item = &'static mut ComponentData> {
) -> impl DoubleEndedIterator<Item = &'static mut ComponentData> {
self.iter_components_mut(entry).filter(|c| !c.enabled)
}
pub fn get_first(&self, entry: usize) -> Option<&'static ComponentData> {

View file

@ -78,7 +78,7 @@ impl EntityManager {
&self,
tag_manager: &TagManager<u16>,
tag: &StdString,
) -> impl Iterator<Item = &'static Entity> {
) -> impl DoubleEndedIterator<Item = &'static Entity> {
let n = *tag_manager.tag_indices.get(tag).unwrap();
self.entity_buckets
.get(n as usize)
@ -91,7 +91,7 @@ impl EntityManager {
&mut self,
tag_manager: &TagManager<u16>,
tag: &StdString,
) -> impl Iterator<Item = &'static mut Entity> {
) -> impl DoubleEndedIterator<Item = &'static mut Entity> {
let n = *tag_manager.tag_indices.get(tag).unwrap();
self.entity_buckets
.get_mut(n as usize)
@ -118,7 +118,7 @@ impl EntityManager {
&self,
tag_manager: &TagManager<u16>,
tag: &StdString,
) -> impl Iterator<Item = &'static Entity> {
) -> impl DoubleEndedIterator<Item = &'static Entity> {
unsafe {
if let Some(n) = tag_manager.tag_indices.get(tag).copied()
&& let Some(v) = self.entity_buckets.get(n as usize)
@ -135,7 +135,7 @@ impl EntityManager {
&mut self,
tag_manager: &TagManager<u16>,
tag: &StdString,
) -> impl Iterator<Item = &'static mut Entity> {
) -> impl DoubleEndedIterator<Item = &'static mut Entity> {
unsafe {
if let Some(n) = tag_manager.tag_indices.get(tag).copied()
&& let Some(v) = self.entity_buckets.get_mut(n as usize)
@ -148,19 +148,21 @@ impl EntityManager {
.filter_map(|e| e.as_mut())
}
}
pub fn iter_entities(&self) -> impl Iterator<Item = &'static Entity> {
pub fn iter_entities(&self) -> impl DoubleEndedIterator<Item = &'static Entity> {
self.entities
.as_ref()
.iter()
.filter_map(|c| unsafe { c.as_ref() })
}
pub fn iter_entities_mut(&mut self) -> impl Iterator<Item = &'static mut Entity> {
pub fn iter_entities_mut(&mut self) -> impl DoubleEndedIterator<Item = &'static mut Entity> {
self.entities
.as_mut()
.iter_mut()
.filter_map(|c| unsafe { c.as_mut() })
}
pub fn iter_component_buffers(&self) -> impl Iterator<Item = &'static ComponentBuffer> {
pub fn iter_component_buffers(
&self,
) -> impl DoubleEndedIterator<Item = &'static ComponentBuffer> {
self.component_buffers
.as_ref()
.iter()
@ -168,7 +170,7 @@ impl EntityManager {
}
pub fn iter_component_buffers_mut(
&mut self,
) -> impl Iterator<Item = &'static mut ComponentBuffer> {
) -> impl DoubleEndedIterator<Item = &'static mut ComponentBuffer> {
self.component_buffers
.as_mut()
.iter_mut()
@ -178,7 +180,7 @@ impl EntityManager {
&self,
entry: usize,
component_type_manager: &ComponentTypeManager,
) -> impl Iterator<Item = &'static C> {
) -> impl DoubleEndedIterator<Item = &'static C> {
let index = component_type_manager
.component_buffer_indices
.get(C::STD_NAME)
@ -194,7 +196,7 @@ impl EntityManager {
&mut self,
entry: usize,
component_type_manager: &mut ComponentTypeManager,
) -> impl Iterator<Item = &'static mut C> {
) -> impl DoubleEndedIterator<Item = &'static mut C> {
component_type_manager
.get_mut::<C>(self)
.iter_components_mut(entry)
@ -204,7 +206,7 @@ impl EntityManager {
&self,
entry: usize,
component_type_manager: &ComponentTypeManager,
) -> impl Iterator<Item = &'static C> {
) -> impl DoubleEndedIterator<Item = &'static C> {
component_type_manager
.get::<C>(self)
.iter_enabled_components(entry)
@ -214,7 +216,7 @@ impl EntityManager {
&mut self,
entry: usize,
component_type_manager: &mut ComponentTypeManager,
) -> impl Iterator<Item = &'static mut C> {
) -> impl DoubleEndedIterator<Item = &'static mut C> {
component_type_manager
.get_mut::<C>(self)
.iter_enabled_components_mut(entry)
@ -224,7 +226,7 @@ impl EntityManager {
&self,
entry: usize,
component_type_manager: &ComponentTypeManager,
) -> impl Iterator<Item = &'static C> {
) -> impl DoubleEndedIterator<Item = &'static C> {
component_type_manager
.get::<C>(self)
.iter_disabled_components(entry)
@ -234,14 +236,15 @@ impl EntityManager {
&mut self,
entry: usize,
component_type_manager: &mut ComponentTypeManager,
) -> impl Iterator<Item = &'static mut C> {
) -> impl DoubleEndedIterator<Item = &'static mut C> {
component_type_manager
.get_mut::<C>(self)
.iter_disabled_components_mut(entry)
.map(|c| unsafe { mem::transmute(c) })
}
#[allow(clippy::mut_from_ref)]
pub fn create_component<C: Component + 'static>(
&mut self,
&self,
entity: &mut Entity,
max_component: &mut usize,
component_type_manager: &mut ComponentTypeManager,
@ -332,29 +335,35 @@ impl EntityManager {
.get_first_disabled_mut(entry)
.map(|c| unsafe { mem::transmute(c) })
}
pub fn iter_every_component(&self) -> impl Iterator<Item = &'static ComponentData> {
pub fn iter_every_component(&self) -> impl DoubleEndedIterator<Item = &'static ComponentData> {
self.iter_component_buffers()
.flat_map(move |c| c.iter_every_component())
}
pub fn iter_every_component_mut(&mut self) -> impl Iterator<Item = &'static mut ComponentData> {
pub fn iter_every_component_mut(
&mut self,
) -> impl DoubleEndedIterator<Item = &'static mut ComponentData> {
self.iter_component_buffers_mut()
.flat_map(move |c| c.iter_every_component_mut())
}
pub fn iter_all_components(
&self,
entry: usize,
) -> impl Iterator<Item = &'static ComponentData> {
) -> impl DoubleEndedIterator<Item = &'static ComponentData> {
self.iter_component_buffers()
.flat_map(move |c| c.iter_components(entry))
}
pub fn iter_all_components_mut(
&mut self,
entry: usize,
) -> impl Iterator<Item = &'static mut ComponentData> {
) -> impl DoubleEndedIterator<Item = &'static mut ComponentData> {
self.iter_component_buffers_mut()
.flat_map(move |c| c.iter_components_mut(entry))
}
pub fn iter_in_radius(&self, pos: Vec2, radius: f32) -> impl Iterator<Item = &'static Entity> {
pub fn iter_in_radius(
&self,
pos: Vec2,
radius: f32,
) -> impl DoubleEndedIterator<Item = &'static Entity> {
self.entities
.as_ref()
.iter()
@ -367,7 +376,7 @@ impl EntityManager {
radius: f32,
tag: &StdString,
tag_manager: &TagManager<u16>,
) -> impl Iterator<Item = &'static Entity> {
) -> impl DoubleEndedIterator<Item = &'static Entity> {
if let Some(tag) = tag_manager.tag_indices.get(tag).copied()
&& let Some(ents) = self.entity_buckets.get(tag as usize)
{
@ -383,7 +392,7 @@ impl EntityManager {
&mut self,
pos: Vec2,
radius: f32,
) -> impl Iterator<Item = &'static mut Entity> {
) -> impl DoubleEndedIterator<Item = &'static mut Entity> {
self.entities
.as_mut()
.iter_mut()
@ -396,7 +405,7 @@ impl EntityManager {
radius: f32,
tag: &StdString,
tag_manager: &TagManager<u16>,
) -> impl Iterator<Item = &'static mut Entity> {
) -> impl DoubleEndedIterator<Item = &'static mut Entity> {
if let Some(tag) = tag_manager.tag_indices.get(tag).copied()
&& let Some(ents) = self.entity_buckets.get_mut(tag as usize)
{

View file

@ -548,7 +548,7 @@ impl AsRef<[CellReactionBuf]> for ReactionLookupTable {
}
impl ReactionLookupTable {
pub fn iter(&self) -> impl Iterator<Item = &'static [CellReaction]> {
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &'static [CellReaction]> {
self.as_ref()
.iter()
.map(|b| unsafe { slice::from_raw_parts(b.base, b.len) })