Skip to content

Commit

Permalink
Improve docstrings and names
Browse files Browse the repository at this point in the history
Thanks Nilirad!
  • Loading branch information
sixfold-origami committed Jul 1, 2022
1 parent 5ba438b commit 288a786
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions crates/bevy_ecs/src/world/archetype_invariants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ use crate::{component::ComponentId, prelude::Bundle, world::World};
/// while `B2` is used in the `consequence`.
/// If only a single generic is provided, these types are the same.
///
/// When added to the [`World`], archetype invariants behave like [`assert!`];
/// all archetype invariants must be true for every entity in the [`World`].
/// When added to the [`World`], archetype invariants behave like [`assert!`].
/// Archetype invariants are checked each time [`Archetypes`](crate::archetype::Archetypes) is modified;
/// this can occur on component addition, component removal, and entity spawning.
///
/// Archetypes are only modified when a novel archetype (set of components) is seen for the first time;
/// swapping between existing archetypes will not trigger these checks.
#[derive(Clone, Debug, PartialEq)]
pub struct ArchetypeInvariant<B1: Bundle, B2: Bundle = B1> {
/// For all entities where the predicate is true
/// Defines which entities this invariant applies to.
/// This is the "if" of the if/then clause.
pub predicate: ArchetypeStatement<B1>,
/// The consequence must also be true
/// Defines what must be true for the entities that this invariant applies to.
/// This is the "then" of the if/then clause.
pub consequence: ArchetypeStatement<B2>,
}

Expand All @@ -44,7 +45,7 @@ impl<B: Bundle> ArchetypeInvariant<B, B> {
///
/// In other words, if any component of this bundle is present, then all of them must be.
#[inline]
pub fn full_bundle() -> Self {
pub fn atomic_bundle() -> Self {
Self {
predicate: ArchetypeStatement::<B>::at_least_one_of(),
consequence: ArchetypeStatement::<B>::all_of(),
Expand Down Expand Up @@ -234,6 +235,7 @@ impl UntypedArchetypeStatement {
}
}

/// A list of [`ArchetypeInvariant`]s to be stored on a [`World`].
#[derive(Default)]
pub struct ArchetypeInvariants {
/// The list of invariants that must be upheld.
Expand Down Expand Up @@ -304,7 +306,7 @@ mod tests {
fn full_bundle_happy() {
let mut world = World::new();

world.add_archetype_invariant(ArchetypeInvariant::<(A, B, C)>::full_bundle());
world.add_archetype_invariant(ArchetypeInvariant::<(A, B, C)>::atomic_bundle());
world.spawn().insert_bundle((A, B, C));
}

Expand All @@ -313,15 +315,15 @@ mod tests {
let mut world = World::new();

world.spawn().insert_bundle((A, B, C));
world.add_archetype_invariant(ArchetypeInvariant::<(A, B, C)>::full_bundle());
world.add_archetype_invariant(ArchetypeInvariant::<(A, B, C)>::atomic_bundle());
}

#[test]
#[should_panic]
fn full_bundle_sad() {
let mut world = World::new();

world.add_archetype_invariant(ArchetypeInvariant::<(A, B, C)>::full_bundle());
world.add_archetype_invariant(ArchetypeInvariant::<(A, B, C)>::atomic_bundle());
world.spawn().insert_bundle((A, B));
}

Expand All @@ -331,6 +333,6 @@ mod tests {
let mut world = World::new();

world.spawn().insert_bundle((A, B));
world.add_archetype_invariant(ArchetypeInvariant::<(A, B, C)>::full_bundle());
world.add_archetype_invariant(ArchetypeInvariant::<(A, B, C)>::atomic_bundle());
}
}

0 comments on commit 288a786

Please sign in to comment.