Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline trivial methods in bevy_hierarchy #11332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/bevy_hierarchy/src/components/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@ impl MapEntities for Children {
// However Children should only ever be set with a real user-defined entities. Its worth looking
// into better ways to handle cases like this.
impl FromWorld for Children {
#[inline]
fn from_world(_world: &mut World) -> Self {
Children(SmallVec::new())
}
}

impl Children {
/// Constructs a [`Children`] component with the given entities.
#[inline]
pub(crate) fn from_entities(entities: &[Entity]) -> Self {
Self(SmallVec::from_slice(entities))
}

/// Swaps the child at `a_index` with the child at `b_index`.
#[inline]
pub fn swap(&mut self, a_index: usize, b_index: usize) {
self.0.swap(a_index, b_index);
}
Expand All @@ -65,6 +68,7 @@ impl Children {
/// For the unstable version, see [`sort_unstable_by`](Children::sort_unstable_by).
///
/// See also [`sort_by_key`](Children::sort_by_key), [`sort_by_cached_key`](Children::sort_by_cached_key).
#[inline]
pub fn sort_by<F>(&mut self, compare: F)
where
F: FnMut(&Entity, &Entity) -> std::cmp::Ordering,
Expand All @@ -80,6 +84,7 @@ impl Children {
/// For the unstable version, see [`sort_unstable_by_key`](Children::sort_unstable_by_key).
///
/// See also [`sort_by`](Children::sort_by), [`sort_by_cached_key`](Children::sort_by_cached_key).
#[inline]
pub fn sort_by_key<K, F>(&mut self, compare: F)
where
F: FnMut(&Entity) -> K,
Expand All @@ -95,6 +100,7 @@ impl Children {
/// For the underlying implementation, see [`slice::sort_by_cached_key`].
///
/// See also [`sort_by`](Children::sort_by), [`sort_by_key`](Children::sort_by_key).
#[inline]
pub fn sort_by_cached_key<K, F>(&mut self, compare: F)
where
F: FnMut(&Entity) -> K,
Expand All @@ -111,6 +117,7 @@ impl Children {
/// For the stable version, see [`sort_by`](Children::sort_by).
///
/// See also [`sort_unstable_by_key`](Children::sort_unstable_by_key).
#[inline]
pub fn sort_unstable_by<F>(&mut self, compare: F)
where
F: FnMut(&Entity, &Entity) -> std::cmp::Ordering,
Expand All @@ -126,6 +133,7 @@ impl Children {
/// For the stable version, see [`sort_by_key`](Children::sort_by_key).
///
/// See also [`sort_unstable_by`](Children::sort_unstable_by).
#[inline]
pub fn sort_unstable_by_key<K, F>(&mut self, compare: F)
where
F: FnMut(&Entity) -> K,
Expand All @@ -138,6 +146,7 @@ impl Children {
impl Deref for Children {
type Target = [Entity];

#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0[..]
}
Expand All @@ -148,6 +157,7 @@ impl<'a> IntoIterator for &'a Children {

type IntoIter = slice::Iter<'a, Entity>;

#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_hierarchy/src/components/parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Parent(pub(crate) Entity);

impl Parent {
/// Gets the [`Entity`] ID of the parent.
#[inline(always)]
pub fn get(&self) -> Entity {
self.0
}
Expand All @@ -37,6 +38,7 @@ impl Parent {
/// for both [`Children`] & [`Parent`] that is agnostic to edge direction.
///
/// [`Children`]: super::children::Children
#[inline(always)]
pub fn as_slice(&self) -> &[Entity] {
std::slice::from_ref(&self.0)
}
Expand All @@ -47,6 +49,7 @@ impl Parent {
// However Parent should only ever be set with a real user-defined entity. Its worth looking into
// better ways to handle cases like this.
impl FromWorld for Parent {
#[inline(always)]
fn from_world(_world: &mut World) -> Self {
Parent(Entity::PLACEHOLDER)
}
Expand All @@ -61,6 +64,7 @@ impl MapEntities for Parent {
impl Deref for Parent {
type Target = Entity;

#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
Expand Down