From 0181d40d83c7e12a3e54846c60b7bff3142cbe0a Mon Sep 17 00:00:00 2001 From: iiYese <83026177+iiYese@users.noreply.github.com> Date: Fri, 22 Sep 2023 07:27:58 +0100 Subject: [PATCH] Add as_slice to parent (#9871) # Objective - Make it possible to write APIs that require a type or homogenous storage for both `Children` & `Parent` that is agnostic to edge direction. ## Solution - Add a way to get the `Entity` from `Parent` as a slice. --------- Co-authored-by: Alice Cecile Co-authored-by: Joseph <21144246+JoJoJet@users.noreply.github.com> --- crates/bevy_hierarchy/src/components/parent.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/bevy_hierarchy/src/components/parent.rs b/crates/bevy_hierarchy/src/components/parent.rs index 7bccf33ed6e1e..68ba8c7c91ed0 100644 --- a/crates/bevy_hierarchy/src/components/parent.rs +++ b/crates/bevy_hierarchy/src/components/parent.rs @@ -23,6 +23,16 @@ impl Parent { pub fn get(&self) -> Entity { self.0 } + + /// Gets the parent [`Entity`] as a slice of length 1. + /// + /// Useful for making APIs that require a type or homogenous storage + /// for both [`Children`] & [`Parent`] that is agnostic to edge direction. + /// + /// [`Children`]: super::children::Children + pub fn as_slice(&self) -> &[Entity] { + std::slice::from_ref(&self.0) + } } // TODO: We need to impl either FromWorld or Default so Parent can be registered as Reflect.