Skip to content

Commit 161ca26

Browse files
committed
Added From implementations to convert from Entity{Ref,Mut}Except to FilteredEntity{Ref,Mut}.
1 parent 4d331dd commit 161ca26

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3287,6 +3287,24 @@ impl<'a> From<&'a EntityWorldMut<'_>> for FilteredEntityRef<'a> {
32873287
}
32883288
}
32893289

3290+
impl<'a, B: Bundle> From<&'a EntityRefExcept<'_, B>> for FilteredEntityRef<'a> {
3291+
fn from(value: &'a EntityRefExcept<'_, B>) -> Self {
3292+
// SAFETY:
3293+
// - The FilteredEntityRef has the same component access as the given EntityRefExcept.
3294+
unsafe {
3295+
let mut access = Access::default();
3296+
access.read_all();
3297+
let components = value.entity.world().components();
3298+
B::get_component_ids(components, &mut |maybe_id| {
3299+
if let Some(id) = maybe_id {
3300+
access.remove_component_read(id);
3301+
}
3302+
});
3303+
FilteredEntityRef::new(value.entity, access)
3304+
}
3305+
}
3306+
}
3307+
32903308
impl PartialEq for FilteredEntityRef<'_> {
32913309
fn eq(&self, other: &Self) -> bool {
32923310
self.entity() == other.entity()
@@ -3612,6 +3630,24 @@ impl<'a> From<&'a mut EntityWorldMut<'_>> for FilteredEntityMut<'a> {
36123630
}
36133631
}
36143632

3633+
impl<'a, B: Bundle> From<&'a EntityMutExcept<'_, B>> for FilteredEntityMut<'a> {
3634+
fn from(value: &'a EntityMutExcept<'_, B>) -> Self {
3635+
// SAFETY:
3636+
// - The FilteredEntityMut has the same component access as the given EntityMutExcept.
3637+
unsafe {
3638+
let mut access = Access::default();
3639+
access.write_all();
3640+
let components = value.entity.world().components();
3641+
B::get_component_ids(components, &mut |maybe_id| {
3642+
if let Some(id) = maybe_id {
3643+
access.remove_component_read(id);
3644+
}
3645+
});
3646+
FilteredEntityMut::new(value.entity, access)
3647+
}
3648+
}
3649+
}
3650+
36153651
impl PartialEq for FilteredEntityMut<'_> {
36163652
fn eq(&self, other: &Self) -> bool {
36173653
self.entity() == other.entity()
@@ -3826,11 +3862,11 @@ where
38263862
}
38273863
}
38283864

3829-
impl<'a, B> From<&EntityMutExcept<'a, B>> for EntityRefExcept<'a, B>
3865+
impl<'a, B> From<&'a EntityMutExcept<'_, B>> for EntityRefExcept<'a, B>
38303866
where
38313867
B: Bundle,
38323868
{
3833-
fn from(entity: &EntityMutExcept<'a, B>) -> Self {
3869+
fn from(entity: &'a EntityMutExcept<'_, B>) -> Self {
38343870
// SAFETY: All accesses that `EntityRefExcept` provides are also
38353871
// accesses that `EntityMutExcept` provides.
38363872
unsafe { EntityRefExcept::new(entity.entity) }

0 commit comments

Comments
 (0)