diff --git a/src/fetch/relations.rs b/src/fetch/relations.rs index 9f9dd1f..7ec3a93 100644 --- a/src/fetch/relations.rs +++ b/src/fetch/relations.rs @@ -47,19 +47,15 @@ where fn access(&self, data: FetchAccessData, dst: &mut Vec) { let relation = self.relation.id(); - dst.extend(data.arch.cells().keys().filter_map(move |k| { - if k.target.is_some() && k.id == relation { - return Some(Access { - kind: AccessKind::Archetype { - id: data.arch_id, - component: *k, - }, - mutable: false, - }); - } - - None - })) + let val = data.arch.relations_like(relation).map(|v| Access { + kind: AccessKind::Archetype { + id: data.arch_id, + component: *v.0, + }, + mutable: false, + }); + + dst.extend(val); } fn describe(&self, f: &mut Formatter) -> fmt::Result { @@ -145,7 +141,7 @@ pub fn nth_relation(relation: impl RelationExt, n: usize) } } -/// Returns a the nth relation of a specified type +/// Returns the *nth* relation of a specified type #[derive(Debug, Clone)] pub struct NthRelation { relation: Relation, @@ -163,7 +159,7 @@ where fn prepare(&self, data: FetchPrepareData<'w>) -> Option { let borrow = data .arch - .relations_like(self.relation.id()) + .relations_like(self.relation.id) .nth(self.n) .map(|(desc, cell)| (desc.target.unwrap(), cell.borrow()))?; @@ -176,19 +172,19 @@ where fn access(&self, data: FetchAccessData, dst: &mut Vec) { let relation = self.relation.id; - dst.extend(data.arch.cells().keys().filter_map(move |k| { - if k.target.is_some() && k.id == relation { - return Some(Access { - kind: AccessKind::Archetype { - id: data.arch_id, - component: *k, - }, - mutable: false, - }); - } - - None - })) + let val = data + .arch + .relations_like(relation) + .nth(self.n) + .map(|v| Access { + kind: AccessKind::Archetype { + id: data.arch_id, + component: *v.0, + }, + mutable: false, + }); + + dst.extend(val); } fn describe(&self, f: &mut Formatter) -> fmt::Result { diff --git a/tests/relations.rs b/tests/relations.rs index ae17160..c9924ae 100644 --- a/tests/relations.rs +++ b/tests/relations.rs @@ -207,13 +207,16 @@ fn many_detach() { Query::new(( entity_ids(), nth_relation(child_of, 0), - // nth_relation(child_of, 2).opt() + nth_relation(child_of, 2).opt() )) .borrow(&world) .iter() .sorted() .collect_vec(), - [(child1, (parent, &"first")), (child2, (parent, &"first"))] + [ + (child1, (parent, &"first"), Some((parent2, &"third"))), + (child2, (parent, &"first"), None) + ] ); // ANCHOR_END: many_to_many // ANCHOR: query