Skip to main content

Children

Struct Children 

Source
pub struct Children { /* private fields */ }
Expand description

Component containing a list of child entities.

This component stores references to all immediate children of an entity. The order of children is preserved and can be significant for rendering order or other order-dependent operations.

§Capacity and Performance

Internally uses a Vec<Entity>, so:

  • Adding children is O(1) amortized
  • Removing children is O(n) where n is the number of children
  • Iteration is cache-friendly

For entities with many children, consider using with_capacity to pre-allocate memory.

§Example

use goud_engine::ecs::Entity;
use goud_engine::ecs::components::Children;

let mut children = Children::new();

let child1 = Entity::new(1, 1);
let child2 = Entity::new(2, 1);

children.push(child1);
children.push(child2);

assert_eq!(children.len(), 2);
assert!(children.contains(child1));

Implementations§

Source§

impl Children

Source

pub fn new() -> Children

Creates an empty Children component.

Source

pub fn with_capacity(capacity: usize) -> Children

Creates a Children component with pre-allocated capacity.

Use this when you know approximately how many children the entity will have.

Source

pub fn from_slice(children: &[Entity]) -> Children

Creates a Children component from a slice of entities.

Source

pub fn len(&self) -> usize

Returns the number of children.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no children.

Source

pub fn push(&mut self, child: Entity)

Adds a child entity to the end of the children list.

Source

pub fn insert(&mut self, index: usize, child: Entity)

Inserts a child entity at a specific index.

§Panics

Panics if index > len.

Source

pub fn remove(&mut self, index: usize) -> Entity

Removes and returns the child at the specified index.

§Panics

Panics if index >= len.

Source

pub fn remove_child(&mut self, child: Entity) -> bool

Removes a child entity if it exists, preserving order.

Returns true if the child was found and removed, false otherwise. This is O(n) as it must search for the child and shift elements.

Source

pub fn swap_remove_child(&mut self, child: Entity) -> bool

Removes a child entity using swap-remove (faster but doesn’t preserve order).

Returns true if the child was found and removed, false otherwise. O(n) for the search but O(1) for the actual removal.

Source

pub fn contains(&self, child: Entity) -> bool

Returns true if the given entity is a child.

Source

pub fn get(&self, index: usize) -> Option<Entity>

Returns the child at the given index, if any.

Source

pub fn first(&self) -> Option<Entity>

Returns the first child, if any.

Source

pub fn last(&self) -> Option<Entity>

Returns the last child, if any.

Source

pub fn iter(&self) -> impl Iterator<Item = &Entity>

Returns an iterator over the children.

Source

pub fn index_of(&self, child: Entity) -> Option<usize>

Returns the index of a child entity, if it exists.

Returns Some(index) if found, None otherwise.

Source

pub fn clear(&mut self)

Removes all children.

Source

pub fn as_slice(&self) -> &[Entity]

Returns the children as a slice.

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&Entity) -> bool,

Retains only the children that satisfy the predicate.

§Example
use goud_engine::ecs::Entity;
use goud_engine::ecs::components::Children;

let mut children = Children::from_slice(&[
    Entity::new(1, 1),
    Entity::new(2, 1),
    Entity::new(3, 1),
]);

// Keep only entities with even indices
children.retain(|e| e.index() % 2 == 0);

assert_eq!(children.len(), 1);
assert!(children.contains(Entity::new(2, 1)));
Source

pub fn sort_by_index(&mut self)

Sorts children by their entity index for deterministic ordering.

Source

pub fn sort_by<F>(&mut self, compare: F)
where F: FnMut(&Entity, &Entity) -> Ordering,

Sorts children using a custom comparison function.

Trait Implementations§

Source§

impl Clone for Children

Source§

fn clone(&self) -> Children

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Children

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Children

Source§

fn default() -> Children

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Children

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Children, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Children

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<&[Entity]> for Children

Source§

fn from(children: &[Entity]) -> Children

Converts to this type from the input type.
Source§

impl From<Children> for Vec<Entity>

Source§

fn from(children: Children) -> Vec<Entity>

Converts to this type from the input type.
Source§

impl From<Vec<Entity>> for Children

Source§

fn from(children: Vec<Entity>) -> Children

Converts to this type from the input type.
Source§

impl<'a> IntoIterator for &'a Children

Source§

type Item = &'a Entity

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Entity>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <&'a Children as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Children

Source§

type Item = Entity

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<Entity>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <Children as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Children

Source§

fn eq(&self, other: &Children) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Children

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Component for Children

Source§

impl Eq for Children

Source§

impl StructuralPartialEq for Children

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

impl<T> QueryState for T
where T: Send + Sync + Clone + 'static,

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,