Skip to content

Commit cfce040

Browse files
committed
fix: Revert "feat: single & first entity queries (#386)"
This reverts commit 4d96146.
1 parent 4d96146 commit cfce040

File tree

4 files changed

+51
-695
lines changed

4 files changed

+51
-695
lines changed

framework_crates/bones_ecs/src/components/iterator.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ impl<'a> Iterator for UntypedComponentBitsetIterator<'a> {
3838
type Item = SchemaRef<'a>;
3939
fn next(&mut self) -> Option<Self::Item> {
4040
let max_id = self.components.max_id;
41-
while self.current_id < max_id
42-
&& !(self.bitset.bit_test(self.current_id)
43-
&& self.components.bitset.bit_test(self.current_id))
41+
while !(self.bitset.bit_test(self.current_id)
42+
&& self.components.bitset.bit_test(self.current_id))
43+
&& self.current_id <= max_id
4444
{
4545
self.current_id += 1;
4646
}
47-
let ret = if self.current_id < max_id {
47+
let ret = if self.current_id <= max_id {
4848
// SAFE: Here we are just getting a pointer, not doing anything unsafe with it.
4949
Some(unsafe {
5050
SchemaRef::from_ptr_schema(
@@ -71,11 +71,11 @@ impl<'a> Iterator for UntypedComponentOptionalBitsetIterator<'a> {
7171
fn next(&mut self) -> Option<Self::Item> {
7272
// We stop iterating at bitset length, not component store length, as we want to iterate over
7373
// whole bitset and return None for entities that don't have this optional component.
74-
let max_id = self.bitset.bit_len();
75-
while self.current_id < max_id && !self.bitset.bit_test(self.current_id) {
74+
let max_id = self.bitset.bit_len() - 1;
75+
while !self.bitset.bit_test(self.current_id) && self.current_id <= max_id {
7676
self.current_id += 1;
7777
}
78-
let ret = if self.current_id < max_id {
78+
let ret = if self.current_id <= max_id {
7979
// SAFE: Here we are just getting a pointer, not doing anything unsafe with it.
8080
if self.components.bitset.bit_test(self.current_id) {
8181
Some(Some(unsafe {
@@ -110,13 +110,13 @@ impl<'a> Iterator for UntypedComponentBitsetIteratorMut<'a> {
110110
type Item = SchemaRefMut<'a>;
111111
fn next(&mut self) -> Option<Self::Item> {
112112
let max_id = self.components.max_id;
113-
while self.current_id < max_id
114-
&& !(self.bitset.bit_test(self.current_id)
115-
&& self.components.bitset.bit_test(self.current_id))
113+
while !(self.bitset.bit_test(self.current_id)
114+
&& self.components.bitset.bit_test(self.current_id))
115+
&& self.current_id <= max_id
116116
{
117117
self.current_id += 1;
118118
}
119-
let ret = if self.current_id < max_id {
119+
let ret = if self.current_id <= max_id {
120120
// SAFE: We know that the index is within bounds, and we know that the pointer will be
121121
// valid for the new lifetime.
122122
Some(unsafe {
@@ -144,11 +144,11 @@ impl<'a> Iterator for UntypedComponentOptionalBitsetIteratorMut<'a> {
144144
fn next(&mut self) -> Option<Self::Item> {
145145
// We do not stop iterating at component store length, as we want to iterate over
146146
// whole bitset and return None for entities that don't have this optional component.
147-
let max_id = self.bitset.bit_len();
148-
while self.current_id < max_id && !self.bitset.bit_test(self.current_id) {
147+
let max_id = self.bitset.bit_len() - 1;
148+
while !self.bitset.bit_test(self.current_id) && self.current_id <= max_id {
149149
self.current_id += 1;
150150
}
151-
let ret = if self.current_id < max_id {
151+
let ret = if self.current_id <= max_id {
152152
// SAFE: Here we are just getting a pointer, not doing anything unsafe with it.
153153
if self.components.bitset.bit_test(self.current_id) {
154154
Some(Some(unsafe {

framework_crates/bones_ecs/src/components/typed.rs

Lines changed: 10 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<T: HasSchema> ComponentStore<T> {
8282
self.untyped.get_mut_or_insert(entity, f)
8383
}
8484

85-
/// Get mutable references to the component data for multiple entities at the same time.
85+
/// Get mutable references s to the component data for multiple entities at the same time.
8686
///
8787
/// # Panics
8888
///
@@ -109,27 +109,6 @@ impl<T: HasSchema> ComponentStore<T> {
109109
self.untyped.remove(entity)
110110
}
111111

112-
/// Gets an immutable reference to the component if there is exactly one instance of it.
113-
#[inline]
114-
pub fn get_single_with_bitset(&self, bitset: Rc<BitSetVec>) -> Result<&T, QuerySingleError> {
115-
// SOUND: we know the schema matches.
116-
self.untyped
117-
.get_single_with_bitset(bitset)
118-
.map(|x| unsafe { x.cast_into_unchecked() })
119-
}
120-
121-
/// Gets a mutable reference to the component if there is exactly one instance of it.
122-
#[inline]
123-
pub fn get_single_with_bitset_mut(
124-
&mut self,
125-
bitset: Rc<BitSetVec>,
126-
) -> Result<&mut T, QuerySingleError> {
127-
// SOUND: we know the schema matches.
128-
self.untyped
129-
.get_single_with_bitset_mut(bitset)
130-
.map(|x| unsafe { x.cast_into_mut_unchecked() })
131-
}
132-
133112
/// Iterates immutably over all components of this type.
134113
/// Very fast but doesn't allow joining with other component types.
135114
#[inline]
@@ -157,15 +136,6 @@ impl<T: HasSchema> ComponentStore<T> {
157136
///
158137
/// Automatically implemented for [`ComponentStore`].
159138
pub trait ComponentIterBitset<'a, T: HasSchema> {
160-
/// Gets an immutable reference to the component if there is exactly one instance of it.
161-
fn get_single_with_bitset(&self, bitset: Rc<BitSetVec>) -> Result<&T, QuerySingleError>;
162-
163-
/// Gets a mutable reference to the component if there is exactly one instance of it.
164-
fn get_single_mut_with_bitset(
165-
&mut self,
166-
bitset: Rc<BitSetVec>,
167-
) -> Result<&mut T, QuerySingleError>;
168-
169139
/// Iterates immutably over the components of this type where `bitset`
170140
/// indicates the indices of entities.
171141
/// Slower than `iter()` but allows joining between multiple component types.
@@ -203,27 +173,6 @@ pub trait ComponentIterBitset<'a, T: HasSchema> {
203173
}
204174

205175
impl<'a, T: HasSchema> ComponentIterBitset<'a, T> for ComponentStore<T> {
206-
/// Gets an immutable reference to the component if there is exactly one instance of it.
207-
fn get_single_with_bitset(&self, bitset: Rc<BitSetVec>) -> Result<&T, QuerySingleError> {
208-
// SOUND: we know the schema matches.
209-
fn map<T>(r: SchemaRef) -> &T {
210-
unsafe { r.cast_into_unchecked() }
211-
}
212-
self.untyped.get_single_with_bitset(bitset).map(map)
213-
}
214-
215-
/// Gets a mutable reference to the component if there is exactly one instance of it.
216-
fn get_single_mut_with_bitset(
217-
&mut self,
218-
bitset: Rc<BitSetVec>,
219-
) -> Result<&mut T, QuerySingleError> {
220-
// SOUND: we know the schema matches.
221-
fn map<T>(r: SchemaRefMut) -> &mut T {
222-
unsafe { r.cast_into_mut_unchecked() }
223-
}
224-
self.untyped.get_single_with_bitset_mut(bitset).map(map)
225-
}
226-
227176
/// Iterates immutably over the components of this type where `bitset`
228177
/// indicates the indices of entities.
229178
/// Slower than `iter()` but allows joining between multiple component types.
@@ -257,7 +206,7 @@ impl<'a, T: HasSchema> ComponentIterBitset<'a, T> for ComponentStore<T> {
257206
#[inline]
258207
fn iter_mut_with_bitset(&mut self, bitset: Rc<BitSetVec>) -> ComponentBitsetIteratorMut<T> {
259208
// SOUND: we know the schema matches.
260-
fn map<T>(r: SchemaRefMut) -> &mut T {
209+
fn map<T>(r: SchemaRefMut<'_>) -> &mut T {
261210
unsafe { r.cast_into_mut_unchecked() }
262211
}
263212

@@ -300,16 +249,14 @@ impl<'a, T: HasSchema> ComponentIterBitset<'a, T> for ComponentStore<T> {
300249

301250
#[cfg(test)]
302251
mod tests {
303-
use std::rc::Rc;
304-
305252
use crate::prelude::*;
306253

307-
#[derive(Debug, Clone, PartialEq, Eq, HasSchema, Default)]
308-
#[repr(C)]
309-
struct A(String);
310-
311254
#[test]
312255
fn create_remove_components() {
256+
#[derive(Debug, Clone, PartialEq, Eq, HasSchema, Default)]
257+
#[repr(C)]
258+
struct A(String);
259+
313260
let mut entities = Entities::default();
314261
let e1 = entities.create();
315262
let e2 = entities.create();
@@ -328,6 +275,10 @@ mod tests {
328275

329276
#[test]
330277
fn get_mut_or_insert() {
278+
#[derive(Debug, Clone, PartialEq, Eq, HasSchema, Default)]
279+
#[repr(C)]
280+
struct A(String);
281+
331282
let mut entities = Entities::default();
332283
let e1 = entities.create();
333284

@@ -347,129 +298,4 @@ mod tests {
347298
// Test that existing component is retrieved
348299
assert_eq!(comp.0, "Test2");
349300
}
350-
351-
#[test]
352-
fn single_returns_none_when_empty() {
353-
let storage = ComponentStore::<A>::default();
354-
let bitset = Rc::new({
355-
let mut entities = Entities::default();
356-
entities.create();
357-
entities.bitset().clone()
358-
});
359-
360-
let maybe_comp = storage.get_single_with_bitset(bitset);
361-
362-
assert_eq!(maybe_comp, Err(QuerySingleError::NoEntities));
363-
}
364-
365-
#[test]
366-
fn single_returns_some_single() {
367-
let mut storage = ComponentStore::<A>::default();
368-
let mut entities = Entities::default();
369-
370-
// Create some dummies so that the target entity isn't 0
371-
(0..3).map(|_| entities.create()).count();
372-
373-
let e = entities.create();
374-
let a = A("a".to_string());
375-
storage.insert(e, a.clone());
376-
377-
let bitset = Rc::new(entities.bitset().clone());
378-
379-
let maybe_comp = storage.get_single_with_bitset(bitset);
380-
381-
assert_eq!(maybe_comp, Ok(&a));
382-
}
383-
384-
#[test]
385-
fn single_returns_none_when_more_than_1() {
386-
let mut entities = Entities::default();
387-
let mut storage = ComponentStore::<A>::default();
388-
389-
(0..3)
390-
.map(|i| storage.insert(entities.create(), A(i.to_string())))
391-
.count();
392-
393-
let bitset = Rc::new(entities.bitset().clone());
394-
395-
let maybe_comp = storage.get_single_with_bitset(bitset);
396-
397-
assert_eq!(maybe_comp, Err(QuerySingleError::MultipleEntities));
398-
}
399-
400-
#[test]
401-
fn iter_with_bitset() {
402-
let mut entities = Entities::default();
403-
let mut storage = ComponentStore::<A>::default();
404-
405-
{
406-
let bitset = Rc::new(entities.bitset().clone());
407-
408-
let mut comp_iter = storage.iter_with_bitset(bitset.clone());
409-
assert_eq!(comp_iter.next(), None);
410-
411-
let mut comp_mut_iter = storage.iter_mut_with_bitset(bitset);
412-
assert_eq!(comp_mut_iter.next(), None);
413-
}
414-
415-
{
416-
let e = entities.create();
417-
let mut a = A("e".to_string());
418-
storage.insert(e, a.clone());
419-
420-
let bitset = Rc::new(entities.bitset().clone());
421-
422-
let mut comp_iter = storage.iter_with_bitset(bitset.clone());
423-
assert_eq!(comp_iter.next(), Some(&a));
424-
425-
let mut comp_mut_iter = storage.iter_mut_with_bitset(bitset);
426-
assert_eq!(comp_mut_iter.next(), Some(&mut a));
427-
428-
entities.kill(e);
429-
}
430-
}
431-
432-
#[test]
433-
fn iter_with_bitset_optional() {
434-
let mut entities = Entities::default();
435-
let mut storage = ComponentStore::<A>::default();
436-
437-
{
438-
let bitset = Rc::new(entities.bitset().clone());
439-
440-
let mut comp_iter = storage.iter_with_bitset_optional(bitset.clone());
441-
assert_eq!(comp_iter.next(), None);
442-
443-
let mut comp_mut_iter = storage.iter_mut_with_bitset_optional(bitset);
444-
assert_eq!(comp_mut_iter.next(), None);
445-
}
446-
447-
{
448-
let e = entities.create();
449-
let bitset = Rc::new(entities.bitset().clone());
450-
451-
let mut comp_iter = storage.iter_with_bitset_optional(bitset.clone());
452-
assert_eq!(comp_iter.next(), Some(None));
453-
454-
let mut comp_mut_iter = storage.iter_mut_with_bitset_optional(bitset);
455-
assert_eq!(comp_mut_iter.next(), Some(None));
456-
457-
entities.kill(e);
458-
}
459-
460-
{
461-
let e = entities.create();
462-
let mut a = A("e".to_string());
463-
storage.insert(e, a.clone());
464-
let bitset = Rc::new(entities.bitset().clone());
465-
466-
let mut comp_iter = storage.iter_with_bitset_optional(bitset.clone());
467-
assert_eq!(comp_iter.next(), Some(Some(&a)));
468-
469-
let mut comp_mut_iter = storage.iter_mut_with_bitset_optional(bitset);
470-
assert_eq!(comp_mut_iter.next(), Some(Some(&mut a)));
471-
472-
entities.kill(e);
473-
}
474-
}
475301
}

framework_crates/bones_ecs/src/components/untyped.rs

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,12 @@ impl UntypedComponentStore {
296296
entity: Entity,
297297
f: impl FnOnce() -> T,
298298
) -> &mut T {
299-
if !self.bitset.bit_test(entity.index() as usize) {
299+
if self.bitset.bit_test(entity.index() as usize) {
300+
return self.get_mut(entity).unwrap();
301+
} else {
300302
self.insert(entity, f());
303+
self.get_mut(entity).unwrap()
301304
}
302-
self.get_mut(entity).unwrap()
303305
}
304306

305307
/// Get a [`SchemaRefMut`] to the component for the given [`Entity`]
@@ -491,37 +493,6 @@ impl UntypedComponentStore {
491493
}
492494
}
493495

494-
/// Get a reference to the component store if there is exactly one instance of the component.
495-
pub fn get_single_with_bitset(
496-
&self,
497-
bitset: Rc<BitSetVec>,
498-
) -> Result<SchemaRef, QuerySingleError> {
499-
let len = self.bitset().bit_len();
500-
let mut iter = (0..len).filter(|&i| bitset.bit_test(i) && self.bitset().bit_test(i));
501-
let i = iter.next().ok_or(QuerySingleError::NoEntities)?;
502-
if iter.next().is_some() {
503-
return Err(QuerySingleError::MultipleEntities);
504-
}
505-
// TODO: add unchecked variant to avoid redundant validation
506-
self.get_idx(i).ok_or(QuerySingleError::NoEntities)
507-
}
508-
509-
/// Get a mutable reference to the component store if there is exactly one instance of the
510-
/// component.
511-
pub fn get_single_with_bitset_mut(
512-
&mut self,
513-
bitset: Rc<BitSetVec>,
514-
) -> Result<SchemaRefMut, QuerySingleError> {
515-
let len = self.bitset().bit_len();
516-
let mut iter = (0..len).filter(|&i| bitset.bit_test(i) && self.bitset().bit_test(i));
517-
let i = iter.next().ok_or(QuerySingleError::NoEntities)?;
518-
if iter.next().is_some() {
519-
return Err(QuerySingleError::MultipleEntities);
520-
}
521-
// TODO: add unchecked variant to avoid redundant validation
522-
self.get_idx_mut(i).ok_or(QuerySingleError::NoEntities)
523-
}
524-
525496
/// Iterates immutably over all components of this type.
526497
///
527498
/// Very fast but doesn't allow joining with other component types.
@@ -630,8 +601,9 @@ impl<'a> Iterator for UntypedComponentStoreIter<'a> {
630601
if let Some(ptr) = self.store.get_idx(self.idx) {
631602
self.idx += 1;
632603
break Some(ptr);
604+
} else {
605+
self.idx += 1;
633606
}
634-
self.idx += 1;
635607
} else {
636608
break None;
637609
}
@@ -656,8 +628,9 @@ impl<'a> Iterator for UntypedComponentStoreIterMut<'a> {
656628
break Some(unsafe {
657629
SchemaRefMut::from_ptr_schema(ptr.as_ptr(), ptr.schema())
658630
});
631+
} else {
632+
self.idx += 1;
659633
}
660-
self.idx += 1;
661634
} else {
662635
break None;
663636
}

0 commit comments

Comments
 (0)