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
impl Children
Sourcepub fn new() -> Children
pub fn new() -> Children
Creates an empty Children component.
§Example
use goud_engine::ecs::components::Children;
let children = Children::new();
assert!(children.is_empty());Sourcepub fn with_capacity(capacity: usize) -> Children
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 yetSourcepub fn from_slice(children: &[Entity]) -> Children
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);Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn insert(&mut self, index: usize, child: Entity)
pub fn insert(&mut self, index: usize, child: Entity)
Inserts a child entity at a specific index.
§Arguments
index- The index to insert atchild- 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)));Sourcepub fn remove(&mut self, index: usize) -> Entity
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);Sourcepub fn remove_child(&mut self, child: Entity) -> bool
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 removedSourcepub fn swap_remove_child(&mut self, child: Entity) -> bool
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);Sourcepub fn contains(&self, child: Entity) -> bool
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)));Sourcepub fn get(&self, index: usize) -> Option<Entity>
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);Sourcepub fn first(&self) -> Option<Entity>
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);Sourcepub fn last(&self) -> Option<Entity>
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)));Sourcepub fn iter(&self) -> impl Iterator<Item = &Entity>
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);
}Sourcepub fn index_of(&self, child: Entity) -> Option<usize>
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);Sourcepub fn clear(&mut self)
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());Sourcepub fn as_slice(&self) -> &[Entity]
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);Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
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)));Sourcepub fn sort_by_index(&mut self)
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)));Sourcepub fn sort_by<F>(&mut self, compare: F)
pub fn sort_by<F>(&mut self, compare: F)
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<'a> IntoIterator for &'a Children
impl<'a> IntoIterator for &'a Children
Source§impl IntoIterator for Children
impl IntoIterator for Children
impl Component for Children
impl Eq for Children
impl StructuralPartialEq for Children
Auto Trait Implementations§
impl Freeze for Children
impl RefUnwindSafe for Children
impl Send for Children
impl Sync for Children
impl Unpin for Children
impl UnsafeUnpin for Children
impl UnwindSafe for Children
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().