Expect

Struct Expect 

Source
pub struct Expect<T>(/* private fields */);
Expand description

A QueryData decorator which panics if its inner query does not match.

§Usage

As a query parameter, this decorator is useful for preventing systems from silently skipping over entities which may erroneously not match the query.

Consider the following erroneous example:

use bevy::prelude::*;

#[derive(Component)]
struct A;

#[derive(Component)]
struct B;

// A and B are always expected to be inserted together:
#[derive(Bundle)]
struct AB {
    a: A,
    b: B,
}

fn bad_system(mut commands: Commands) {
    commands.spawn(A); // Spawn A without B!
}

fn unsafe_system(q: Query<(&A, &B)>) {
    for _ in q.iter() {
        // An instance of `A` does exist.
        // But because `A` does not exist *with* `B`, this system skips over it silently.
    }
}

This problem can be solved with Expect:

use moonshine_util::expect::Expect;

fn safe_system(q: Query<(&A, Expect<&B>)>) {
    for _ in q.iter() {
       // This system will panic if it finds an instance of `A` without `B`.
    }
}

§Component Requirements

When used as a Component, this decorator will panic if the given component type T does not exist on the entity. This is especially useful as a component requirement:

use moonshine_util::expect::Expect;

#[derive(Component)]
struct A;

#[derive(Component)]
#[require(Expect<A>)]
struct B;

fn unsafe_system(mut commands: Commands) {
   commands.spawn(B); // Spawn B without A! This will panic!
}

Trait Implementations§

Source§

impl<T> Component for Expect<T>
where T: Component,

Source§

const STORAGE_TYPE: StorageType = StorageType::SparseSet

A constant indicating the storage type used for this component.
Source§

type Mutability = Immutable

A marker type to assist Bevy with determining if this component is mutable, or immutable. Mutable components will have Component<Mutability = Mutable>, while immutable components will instead have Component<Mutability = Immutable>. Read more
Source§

fn on_add() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_add ComponentHook for this Component if one is defined.
Source§

fn on_insert() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_insert ComponentHook for this Component if one is defined.
Source§

fn on_replace() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_replace ComponentHook for this Component if one is defined.
Source§

fn on_remove() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_remove ComponentHook for this Component if one is defined.
Source§

fn on_despawn() -> Option<for<'w> fn(DeferredWorld<'w>, HookContext)>

Gets the on_despawn ComponentHook for this Component if one is defined.
Source§

fn register_required_components( _component_id: ComponentId, _required_components: &mut RequiredComponentsRegistrator<'_, '_>, )

Registers required components. Read more
Source§

fn clone_behavior() -> ComponentCloneBehavior

Called when registering this component, allowing to override clone function (or disable cloning altogether) for this component. Read more
Source§

fn map_entities<E>(_this: &mut Self, _mapper: &mut E)
where E: EntityMapper,

Maps the entities on this component using the given EntityMapper. This is used to remap entities in contexts like scenes and entity cloning. When deriving Component, this is populated by annotating fields containing entities with #[entities] Read more
Source§

fn relationship_accessor() -> Option<ComponentRelationshipAccessor<Self>>

Returns ComponentRelationshipAccessor required for working with relationships in dynamic contexts. Read more
Source§

impl<T> Default for Expect<T>
where T: Component,

Source§

fn default() -> Expect<T>

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

impl<T> QueryData for Expect<T>
where T: QueryData,

Source§

const IS_READ_ONLY: bool = true

True if this query is read-only and may not perform mutable access.
Source§

const IS_ARCHETYPAL: bool = T::IS_ARCHETYPAL

Returns true if (and only if) this query data relies strictly on archetypes to limit which entities are accessed by the Query. Read more
Source§

type ReadOnly = Expect<<T as QueryData>::ReadOnly>

The read-only variant of this QueryData, which satisfies the ReadOnlyQueryData trait.
Source§

type Item<'w, 's> = <T as QueryData>::Item<'w, 's>

The item returned by this WorldQuery This will be the data retrieved by the query, and is visible to the end user when calling e.g. Query<Self>::get.
Source§

fn shrink<'wlong, 'wshort, 's>( item: <Expect<T> as QueryData>::Item<'wlong, 's>, ) -> <Expect<T> as QueryData>::Item<'wshort, 's>
where 'wlong: 'wshort,

This function manually implements subtyping for the query items.
Source§

unsafe fn fetch<'w, 's>( state: &'s <Expect<T> as WorldQuery>::State, fetch: &mut <Expect<T> as WorldQuery>::Fetch<'w>, entity: Entity, table_row: TableRow, ) -> Option<<Expect<T> as QueryData>::Item<'w, 's>>

Fetch Self::Item for either the given entity in the current Table, or for the given entity in the current Archetype. This must always be called after WorldQuery::set_table with a table_row in the range of the current Table or after WorldQuery::set_archetype with an entity in the current archetype. Accesses components registered in WorldQuery::update_component_access. Read more
Source§

fn iter_access( state: &<Expect<T> as WorldQuery>::State, ) -> impl Iterator<Item = EcsAccessType<'_>>

Returns an iterator over the access needed by QueryData::fetch. Access conflicts are usually checked in WorldQuery::update_component_access, but in certain cases this method can be useful to implement a way of checking for access conflicts in a non-allocating way.
Source§

fn provide_extra_access( _state: &mut Self::State, _access: &mut Access, _available_access: &Access, )

Offers additional access above what we requested in update_component_access. Implementations may add additional access that is a subset of available_access and does not conflict with anything in access, and must update access to include that access. Read more
Source§

impl<T> WorldQuery for Expect<T>
where T: QueryData,

Source§

const IS_DENSE: bool = T::IS_DENSE

Returns true if (and only if) every table of every archetype matched by this fetch contains all of the matched components. Read more
Source§

type Fetch<'w> = ExpectFetch<'w, T>

Per archetype/table state retrieved by this WorldQuery to compute Self::Item for each entity.
Source§

type State = <T as WorldQuery>::State

State used to construct a Self::Fetch. This will be cached inside QueryState, so it is best to move as much data / computation here as possible to reduce the cost of constructing Self::Fetch.
Source§

fn shrink_fetch<'wlong, 'wshort>( fetch: <Expect<T> as WorldQuery>::Fetch<'wlong>, ) -> <Expect<T> as WorldQuery>::Fetch<'wshort>
where 'wlong: 'wshort,

This function manually implements subtyping for the query fetches.
Source§

unsafe fn init_fetch<'w>( world: UnsafeWorldCell<'w>, state: &<T as WorldQuery>::State, last_run: Tick, this_run: Tick, ) -> ExpectFetch<'w, T>

Creates a new instance of Self::Fetch, by combining data from the World with the cached Self::State. Readonly accesses resources registered in WorldQuery::update_component_access. Read more
Source§

unsafe fn set_archetype<'w>( fetch: &mut ExpectFetch<'w, T>, state: &<T as WorldQuery>::State, archetype: &'w Archetype, table: &'w Table, )

Adjusts internal state to account for the next Archetype. This will always be called on archetypes that match this WorldQuery. Read more
Source§

unsafe fn set_table<'w>( fetch: &mut ExpectFetch<'w, T>, state: &<T as WorldQuery>::State, table: &'w Table, )

Adjusts internal state to account for the next Table. This will always be called on tables that match this WorldQuery. Read more
Source§

fn update_component_access( state: &<T as WorldQuery>::State, access: &mut FilteredAccess, )

Adds any component accesses used by this WorldQuery to access. Read more
Source§

fn get_state( components: &Components, ) -> Option<<Expect<T> as WorldQuery>::State>

Attempts to initialize a State for this WorldQuery type using read-only access to Components.
Source§

fn init_state(world: &mut World) -> <T as WorldQuery>::State

Creates and initializes a State for this WorldQuery type.
Source§

fn matches_component_set( _state: &<T as WorldQuery>::State, _set_contains_id: &impl Fn(ComponentId) -> bool, ) -> bool

Returns true if this query matches a set of components. Otherwise, returns false. Read more
Source§

impl<T> ReadOnlyQueryData for Expect<T>

Auto Trait Implementations§

§

impl<T> Freeze for Expect<T>

§

impl<T> RefUnwindSafe for Expect<T>
where T: RefUnwindSafe,

§

impl<T> Send for Expect<T>
where T: Send,

§

impl<T> Sync for Expect<T>
where T: Sync,

§

impl<T> Unpin for Expect<T>
where T: Unpin,

§

impl<T> UnwindSafe for Expect<T>
where T: UnwindSafe,

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<C> Bundle for C
where C: Component,

Source§

fn component_ids( components: &mut ComponentsRegistrator<'_>, ) -> impl Iterator<Item = ComponentId> + use<C>

Source§

fn get_component_ids( components: &Components, ) -> impl Iterator<Item = Option<ComponentId>>

Return a iterator over this Bundle’s component ids. This will be None if the component has not been registered.
Source§

impl<C> BundleFromComponents for C
where C: Component,

Source§

unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
where F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a>,

Source§

impl<T> CastInto<T> for T
where T: Kind,

Source§

unsafe fn cast(instance: Instance<T>) -> Instance<T>

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<C> DynamicBundle for C
where C: Component,

Source§

type Effect = ()

An operation on the entity that happens after inserting this bundle.
Source§

unsafe fn get_components( ptr: MovingPtr<'_, C>, func: &mut impl FnMut(StorageType, OwningPtr<'_>), ) -> <C as DynamicBundle>::Effect

Moves the components out of the bundle. Read more
Source§

unsafe fn apply_effect( _ptr: MovingPtr<'_, MaybeUninit<C>>, _entity: &mut EntityWorldMut<'_>, )

Applies the after-effects of spawning this bundle. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using default().

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

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

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<T> Kind for T
where T: Component,

Source§

type Filter = With<T>

The QueryFilter which defines this kind.
Source§

fn debug_name() -> String

Returns the debug name of this kind. 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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

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

Source§

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

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,