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.

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

let children = Children::new();
assert!(children.is_empty());
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.

§Arguments
  • capacity - The number of children to pre-allocate space for
§Example
use goud_engine::ecs::components::Children;

let children = Children::with_capacity(100);
assert!(children.is_empty()); // No children yet
Source

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

Creates a Children component from a slice of entities.

§Arguments
  • children - Slice of child entities
§Example
use goud_engine::ecs::Entity;
use goud_engine::ecs::components::Children;

let entities = vec![Entity::new(1, 1), Entity::new(2, 1)];
let children = Children::from_slice(&entities);

assert_eq!(children.len(), 2);
Source

pub fn len(&self) -> usize

Returns the number of children.

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

let children = Children::from_slice(&[Entity::new(1, 1), Entity::new(2, 1)]);
assert_eq!(children.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if there are no children.

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

let children = Children::new();
assert!(children.is_empty());
Source

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

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

§Arguments
  • child - The child entity to add
§Example
use goud_engine::ecs::Entity;
use goud_engine::ecs::components::Children;

let mut children = Children::new();
children.push(Entity::new(1, 1));
assert_eq!(children.len(), 1);
Source

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

Inserts a child entity at a specific index.

§Arguments
  • index - The index to insert at
  • child - The child entity to insert
§Panics

Panics if index > len.

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

let mut children = Children::new();
children.push(Entity::new(1, 1));
children.push(Entity::new(3, 1));
children.insert(1, Entity::new(2, 1)); // Insert at index 1

assert_eq!(children.get(1), Some(Entity::new(2, 1)));
Source

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

Removes and returns the child at the specified index.

§Arguments
  • index - The index of the child to remove
§Returns

The removed child entity.

§Panics

Panics if index >= len.

§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)]);
let removed = children.remove(0);

assert_eq!(removed, Entity::new(1, 1));
assert_eq!(children.len(), 1);
Source

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

Removes a child entity if it exists, preserving order.

§Arguments
  • child - The child entity to remove
§Returns

true if the child was found and removed, false otherwise.

§Performance

This is O(n) as it must search for the child and shift elements.

§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)]);

assert!(children.remove_child(Entity::new(1, 1)));
assert!(!children.remove_child(Entity::new(1, 1))); // Already removed
Source

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

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

§Arguments
  • child - The child entity to remove
§Returns

true if the child was found and removed, false otherwise.

§Performance

This is O(n) for the search but O(1) for the actual removal. Use this when child order doesn’t matter.

§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),
]);

children.swap_remove_child(Entity::new(1, 1));
// Order may have changed, but length is now 2
assert_eq!(children.len(), 2);
Source

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

Returns true if the given entity is a child.

§Arguments
  • child - The entity to check
§Example
use goud_engine::ecs::Entity;
use goud_engine::ecs::components::Children;

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

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

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

Returns the child at the given index, if any.

§Arguments
  • index - The index of the child to get
§Returns

Some(entity) if the index is valid, None otherwise.

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

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

assert_eq!(children.get(0), Some(Entity::new(1, 1)));
assert_eq!(children.get(10), None);
Source

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

Returns the first child, if any.

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

let children = Children::from_slice(&[Entity::new(1, 1), Entity::new(2, 1)]);
assert_eq!(children.first(), Some(Entity::new(1, 1)));

let empty = Children::new();
assert_eq!(empty.first(), None);
Source

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

Returns the last child, if any.

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

let children = Children::from_slice(&[Entity::new(1, 1), Entity::new(2, 1)]);
assert_eq!(children.last(), Some(Entity::new(2, 1)));
Source

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

Returns an iterator over the children.

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

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

for child in children.iter() {
    println!("Child: {:?}", child);
}
Source

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

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

§Arguments
  • child - The entity to find
§Returns

Some(index) if found, None otherwise.

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

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

assert_eq!(children.index_of(Entity::new(2, 1)), Some(1));
assert_eq!(children.index_of(Entity::new(99, 1)), None);
Source

pub fn clear(&mut self)

Removes all children.

§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)]);
children.clear();

assert!(children.is_empty());
Source

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

Returns the children as a slice.

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

let children = Children::from_slice(&[Entity::new(1, 1), Entity::new(2, 1)]);
let slice = children.as_slice();

assert_eq!(slice.len(), 2);
Source

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

Retains only the children that satisfy the predicate.

§Arguments
  • f - The predicate function
§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.

This can be useful for deterministic ordering.

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

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

children.sort_by_index();

assert_eq!(children.get(0), Some(Entity::new(1, 1)));
assert_eq!(children.get(1), Some(Entity::new(2, 1)));
assert_eq!(children.get(2), Some(Entity::new(3, 1)));
Source

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

Sorts children using a custom comparison function.

§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),
]);

// Sort in reverse order
children.sort_by(|a, b| b.index().cmp(&a.index()));

assert_eq!(children.get(0), Some(Entity::new(3, 1)));

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 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 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<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<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,