Skip to content

Commit 698b2d5

Browse files
committed
fixed up iterator and test
1 parent 97cb45e commit 698b2d5

File tree

1 file changed

+11
-52
lines changed

1 file changed

+11
-52
lines changed

crates/bevy_ecs/src/relationship/relationship_source_collection.rs

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
117117
}
118118

119119
impl RelationshipSourceCollection for Entity {
120-
type SourceIter<'a> = core::iter::Copied<core::iter::Once<Entity>>;
120+
type SourceIter<'a> = core::iter::Once<Entity>;
121121

122122
fn with_capacity(_capacity: usize) -> Self {
123123
Entity::PLACEHOLDER
@@ -142,35 +142,6 @@ impl RelationshipSourceCollection for Entity {
142142
}
143143
}
144144

145-
impl RelationshipSourceCollection for Option<Entity> {
146-
type SourceIter<'a> = core::iter::Copied<core::option::Iter<'a, Entity>>;
147-
148-
fn with_capacity(_capacity: usize) -> Self {
149-
None
150-
}
151-
152-
fn add(&mut self, entity: Entity) {
153-
*self = Some(entity);
154-
}
155-
156-
fn remove(&mut self, entity: Entity) {
157-
if self.is_none_or(|e| e == entity) {
158-
*self = None
159-
}
160-
}
161-
162-
fn iter(&self) -> Self::SourceIter<'_> {
163-
self.iter().copied()
164-
}
165-
166-
fn len(&self) -> usize {
167-
match self {
168-
Some(_) => 1,
169-
None => 0,
170-
}
171-
}
172-
}
173-
174145
#[cfg(test)]
175146
mod tests {
176147
use super::*;
@@ -241,35 +212,23 @@ mod tests {
241212
}
242213

243214
#[test]
244-
fn option_entity_relationship_source_collection() {
215+
fn entity_relationship_source_collection() {
245216
#[derive(Component)]
246-
#[relationship(relationship_target = Behind)]
247-
struct InFront(Entity);
217+
#[relationship(relationship_target = RelTarget)]
218+
struct Rel(Entity);
248219

249220
#[derive(Component)]
250-
#[relationship_target(relationship = InFront)]
251-
struct Behind(Option<Entity>);
221+
#[relationship_target(relationship = Rel)]
222+
struct RelTarget(Entity);
252223

253224
let mut world = World::new();
254225
let a = world.spawn_empty().id();
255226
let b = world.spawn_empty().id();
256227

257-
// in front test
258-
// world.entity_mut(a).insert(InFront(b));
259-
//
260-
// assert_eq!(world.get::<Behind>(b).unwrap().0, Some(a));
261-
// world.despawn(b);
262-
// assert!(world.get::<InFront>(a).is_none());
263-
//
264-
// let c = world.spawn_empty().id();
265-
// world.despawn(a);
266-
// assert!(world.get::<Behind>(c).is_none());
267-
268-
// test adding target and see if relationship gets added?
269-
world.entity_mut(b).insert(Behind(Some(a)));
270-
271-
assert_eq!(world.get::<InFront>(b).unwrap().0, a);
272-
// world.despawn(b);
273-
// assert!(world.get::<InFront>(a).is_none());
228+
world.entity_mut(a).insert(Rel(b));
229+
230+
let rel_target = world.get::<RelTarget>(b).unwrap();
231+
let collection = rel_target.collection();
232+
assert_eq!(collection, &a);
274233
}
275234
}

0 commit comments

Comments
 (0)