Skip to content

Commit

Permalink
fix: nth_relation access granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Oct 18, 2023
1 parent 2ee705b commit 0c136d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
52 changes: 24 additions & 28 deletions src/fetch/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,15 @@ where

fn access(&self, data: FetchAccessData, dst: &mut Vec<Access>) {
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 {
Expand Down Expand Up @@ -145,7 +141,7 @@ pub fn nth_relation<T: ComponentValue>(relation: impl RelationExt<T>, 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<T: ComponentValue> {
relation: Relation<T>,
Expand All @@ -163,7 +159,7 @@ where
fn prepare(&self, data: FetchPrepareData<'w>) -> Option<Self::Prepared> {
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()))?;

Expand All @@ -176,19 +172,19 @@ where

fn access(&self, data: FetchAccessData, dst: &mut Vec<Access>) {
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 {
Expand Down
7 changes: 5 additions & 2 deletions tests/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c136d9

Please sign in to comment.