pub struct Instance<T: Kind>(/* private fields */);Expand description
Represents an Entity of Kind T.
Instance<Any> is functionally equivalent to an entity.
§Usage
An Instance<T> can be used to access entities in a “kind-safe” manner to improve safety and readability.
This type is designed to behave exactly like an Entity.
This means you may use it as a Query parameter, pass it to Commands to access InstanceCommands<T>,
or store it as a type-safe reference to an Entity.
Note that an Instance<T> has 'static lifetime and does not contain any Component data.
It only contains type information.
§Example
#[derive(Component)]
struct Apple;
#[derive(Component)]
struct Orange;
struct Fruit;
impl Kind for Fruit {
type Filter = Or<(With<Apple>, With<Orange>)>;
}
#[derive(Resource, Deref, DerefMut)]
struct FruitBasket(Vec<Instance<Fruit>>);
fn collect_fruits(mut basket: ResMut<FruitBasket>, fruits: Query<Instance<Fruit>>) {
for fruit in fruits.iter() {
println!("{fruit:?}");
basket.push(fruit);
}
}
Implementations§
Source§impl<T: Kind> Instance<T>
impl<T: Kind> Instance<T>
Sourcepub const PLACEHOLDER: Self
pub const PLACEHOLDER: Self
Same as Entity::PLACEHOLDER, but for an Instance<T>.
Sourcepub unsafe fn from_entity_unchecked(entity: Entity) -> Self
pub unsafe fn from_entity_unchecked(entity: Entity) -> Self
Creates a new instance of kind T from some Entity.
§Usage
This function is useful when you know an Entity is of a specific kind and you
need an Instance<T> with no way to validate it.
See Instance::from_entity for a safer alternative.
§Safety
Assumes entity is a valid instance of kind T.
§Example
#[derive(Component)]
struct Apple;
fn init_apple(entity: Entity, commands: &mut Commands) -> Instance<Apple> {
commands.entity(entity).insert(Apple);
// SAFE: `entity` will be a valid instance of `Apple`.
unsafe { Instance::from_entity_unchecked(entity) }
}Sourcepub fn cast_into<U: Kind>(self) -> Instance<U>where
T: CastInto<U>,
pub fn cast_into<U: Kind>(self) -> Instance<U>where
T: CastInto<U>,
Converts this instance into an instance of another kind Kind U.
§Usage
A kind T is safety convertible to another kind U if T implements CastInto<U>.
See [kind] macro for usage examples.
Sourcepub fn cast_into_any(self) -> Instance<Any>
pub fn cast_into_any(self) -> Instance<Any>
Converts this instance into an instance of Kind Any.
§Usage
Any Instance<T> can be safely cast into an Instance<Any> using this function.
Sourcepub unsafe fn cast_into_unchecked<U: Kind>(self) -> Instance<U>
pub unsafe fn cast_into_unchecked<U: Kind>(self) -> Instance<U>
Converts this instance into an instance of another kind Kind U without any validation.
§Usage
This function is useful when you know an Instance<T> is convertible to a specific type and you
need an Instance<U> with no way to validate it.
Always prefer to explicitly declare safe casts using [kind] macro and use Instance::cast_into instead of this.
§Safety
Assumes this instance is also a valid Instance<U>.
Trait Implementations§
Source§impl<'a, T: Kind> From<&InstanceCommands<'a, T>> for Instance<T>
impl<'a, T: Kind> From<&InstanceCommands<'a, T>> for Instance<T>
Source§fn from(commands: &InstanceCommands<'a, T>) -> Self
fn from(commands: &InstanceCommands<'a, T>) -> Self
Source§impl<T: Component> From<&InstanceMut<'_, T>> for Instance<T>
impl<T: Component> From<&InstanceMut<'_, T>> for Instance<T>
Source§fn from(item: &InstanceMut<'_, T>) -> Self
fn from(item: &InstanceMut<'_, T>) -> Self
Source§impl<T: Component> From<&InstanceRef<'_, T>> for Instance<T>
impl<T: Component> From<&InstanceRef<'_, T>> for Instance<T>
Source§fn from(item: &InstanceRef<'_, T>) -> Self
fn from(item: &InstanceRef<'_, T>) -> Self
Source§impl<'a, T: Kind> From<InstanceCommands<'a, T>> for Instance<T>
impl<'a, T: Kind> From<InstanceCommands<'a, T>> for Instance<T>
Source§fn from(commands: InstanceCommands<'a, T>) -> Self
fn from(commands: InstanceCommands<'a, T>) -> Self
Source§impl<T: Component> From<InstanceMut<'_, T>> for Instance<T>
impl<T: Component> From<InstanceMut<'_, T>> for Instance<T>
Source§fn from(item: InstanceMut<'_, T>) -> Self
fn from(item: InstanceMut<'_, T>) -> Self
Source§impl<T: Component> From<InstanceRef<'_, T>> for Instance<T>
impl<T: Component> From<InstanceRef<'_, T>> for Instance<T>
Source§fn from(item: InstanceRef<'_, T>) -> Self
fn from(item: InstanceRef<'_, T>) -> Self
Source§impl<T> FromReflect for Instance<T>
impl<T> FromReflect for Instance<T>
Source§fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self>
fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self>
Self from a reflected value.Source§fn take_from_reflect(
reflect: Box<dyn PartialReflect>,
) -> Result<Self, Box<dyn PartialReflect>>
fn take_from_reflect( reflect: Box<dyn PartialReflect>, ) -> Result<Self, Box<dyn PartialReflect>>
Self using,
constructing the value using from_reflect if that fails. Read moreSource§impl<T> GetTypeRegistration for Instance<T>
impl<T> GetTypeRegistration for Instance<T>
Source§fn get_type_registration() -> TypeRegistration
fn get_type_registration() -> TypeRegistration
TypeRegistration for this type.Source§fn register_type_dependencies(registry: &mut TypeRegistry)
fn register_type_dependencies(registry: &mut TypeRegistry)
Source§impl<T: Kind> MapEntities for Instance<T>
impl<T: Kind> MapEntities for Instance<T>
Source§fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M)
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M)
Source§impl<T: Kind> Ord for Instance<T>
impl<T: Kind> Ord for Instance<T>
Source§impl<T: Kind> PartialOrd for Instance<T>
impl<T: Kind> PartialOrd for Instance<T>
Source§impl<T> PartialReflect for Instance<T>
impl<T> PartialReflect for Instance<T>
Source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
Source§fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
Source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Source§fn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
Source§fn try_into_reflect(
self: Box<Self>,
) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
fn try_into_reflect( self: Box<Self>, ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
Source§fn try_as_reflect(&self) -> Option<&dyn Reflect>
fn try_as_reflect(&self) -> Option<&dyn Reflect>
Source§fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
Source§fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
Source§fn as_partial_reflect(&self) -> &dyn PartialReflect
fn as_partial_reflect(&self) -> &dyn PartialReflect
Source§fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
Source§fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool>
fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool>
Source§fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
Self using reflection. Read moreSource§fn apply(&mut self, value: &(dyn PartialReflect + 'static))
fn apply(&mut self, value: &(dyn PartialReflect + 'static))
Source§fn clone_value(&self) -> Box<dyn PartialReflect>
fn clone_value(&self) -> Box<dyn PartialReflect>
reflect_clone. To convert reflected values to dynamic ones, use to_dynamic.Self into its dynamic representation. Read moreSource§fn to_dynamic(&self) -> Box<dyn PartialReflect>
fn to_dynamic(&self) -> Box<dyn PartialReflect>
Source§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Source§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
Source§impl<T: Kind> QueryData for Instance<T>
impl<T: Kind> QueryData for Instance<T>
Source§const IS_READ_ONLY: bool = true
const IS_READ_ONLY: bool = true
Source§type ReadOnly = Instance<T>
type ReadOnly = Instance<T>
QueryData, which satisfies the ReadOnlyQueryData trait.Source§type Item<'a> = Instance<T>
type Item<'a> = Instance<T>
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, 'wshort>(
item: Self::Item<'wlong>,
) -> Self::Item<'wshort>
fn shrink<'wlong: 'wshort, 'wshort>( item: Self::Item<'wlong>, ) -> Self::Item<'wshort>
Source§unsafe fn fetch<'w>(
_fetch: &mut Self::Fetch<'w>,
entity: Entity,
_table_row: TableRow,
) -> Self::Item<'w>
unsafe fn fetch<'w>( _fetch: &mut Self::Fetch<'w>, entity: Entity, _table_row: TableRow, ) -> Self::Item<'w>
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 moreSource§impl<T> Reflect for Instance<T>
impl<T> Reflect for Instance<T>
Source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
&mut dyn Any. Read moreSource§fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
Source§fn as_reflect(&self) -> &dyn Reflect
fn as_reflect(&self) -> &dyn Reflect
Source§fn as_reflect_mut(&mut self) -> &mut dyn Reflect
fn as_reflect_mut(&mut self) -> &mut dyn Reflect
Source§impl<T: Kind> RelationshipSourceCollection for Instance<T>
impl<T: Kind> RelationshipSourceCollection for Instance<T>
Source§type SourceIter<'a> = <Entity as RelationshipSourceCollection>::SourceIter<'a>
type SourceIter<'a> = <Entity as RelationshipSourceCollection>::SourceIter<'a>
iter method. Read moreSource§fn with_capacity(_capacity: usize) -> Self
fn with_capacity(_capacity: usize) -> Self
capacity. Read moreSource§fn reserve(&mut self, additional: usize)
fn reserve(&mut self, additional: usize)
additional more entities to be inserted. Read moreSource§fn remove(&mut self, entity: Entity) -> bool
fn remove(&mut self, entity: Entity) -> bool
entity from the collection. Read moreSource§fn iter(&self) -> Self::SourceIter<'_>
fn iter(&self) -> Self::SourceIter<'_>
Source§fn shrink_to_fit(&mut self)
fn shrink_to_fit(&mut self)
Source§fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>)
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>)
Source§impl<T> TupleStruct for Instance<T>
impl<T> TupleStruct for Instance<T>
Source§fn field(&self, index: usize) -> Option<&dyn PartialReflect>
fn field(&self, index: usize) -> Option<&dyn PartialReflect>
index as a
&dyn Reflect.Source§fn field_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect>
fn field_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect>
index
as a &mut dyn Reflect.Source§fn iter_fields(&self) -> TupleStructFieldIter<'_>
fn iter_fields(&self) -> TupleStructFieldIter<'_>
Source§fn to_dynamic_tuple_struct(&self) -> DynamicTupleStruct
fn to_dynamic_tuple_struct(&self) -> DynamicTupleStruct
DynamicTupleStruct from this tuple struct.Source§fn clone_dynamic(&self) -> DynamicTupleStruct
fn clone_dynamic(&self) -> DynamicTupleStruct
to_dynamic_tuple_struct insteadDynamicTupleStruct.Source§fn get_represented_tuple_struct_info(&self) -> Option<&'static TupleStructInfo>
fn get_represented_tuple_struct_info(&self) -> Option<&'static TupleStructInfo>
None if TypeInfo is not available.Source§impl<T> TypePath for Instance<T>
impl<T> TypePath for Instance<T>
Source§fn type_path() -> &'static str
fn type_path() -> &'static str
Source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
Source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
Source§impl<T: Kind> WorldQuery for Instance<T>
impl<T: Kind> WorldQuery for Instance<T>
Source§const IS_DENSE: bool = <T::Filter as WorldQuery>::IS_DENSE
const IS_DENSE: bool = <T::Filter as WorldQuery>::IS_DENSE
Source§type Fetch<'a> = <<T as Kind>::Filter as WorldQuery>::Fetch<'a>
type Fetch<'a> = <<T as Kind>::Filter as WorldQuery>::Fetch<'a>
WorldQuery to compute Self::Item for each entity.Source§type State = <<T as Kind>::Filter as WorldQuery>::State
type State = <<T as Kind>::Filter as WorldQuery>::State
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, 'wshort>(
fetch: Self::Fetch<'wlong>,
) -> Self::Fetch<'wshort>
fn shrink_fetch<'wlong: 'wshort, 'wshort>( fetch: Self::Fetch<'wlong>, ) -> Self::Fetch<'wshort>
Source§unsafe fn init_fetch<'w>(
world: UnsafeWorldCell<'w>,
state: &Self::State,
last_change_tick: Tick,
change_tick: Tick,
) -> Self::Fetch<'w>
unsafe fn init_fetch<'w>( world: UnsafeWorldCell<'w>, state: &Self::State, last_change_tick: Tick, change_tick: Tick, ) -> Self::Fetch<'w>
Self::Fetch,
by combining data from the World with the cached Self::State.
Readonly accesses resources registered in WorldQuery::update_component_access. Read moreSource§unsafe fn set_archetype<'w>(
fetch: &mut Self::Fetch<'w>,
state: &Self::State,
archetype: &'w Archetype,
table: &'w Table,
)
unsafe fn set_archetype<'w>( fetch: &mut Self::Fetch<'w>, state: &Self::State, archetype: &'w Archetype, table: &'w Table, )
Archetype. This will always be called on
archetypes that match this WorldQuery. Read moreSource§unsafe fn set_table<'w>(
fetch: &mut Self::Fetch<'w>,
state: &Self::State,
table: &'w Table,
)
unsafe fn set_table<'w>( fetch: &mut Self::Fetch<'w>, state: &Self::State, table: &'w Table, )
Table. This will always be called on tables
that match this WorldQuery. Read moreSource§fn update_component_access(
state: &Self::State,
access: &mut FilteredAccess<ComponentId>,
)
fn update_component_access( state: &Self::State, access: &mut FilteredAccess<ComponentId>, )
Source§fn init_state(world: &mut World) -> Self::State
fn init_state(world: &mut World) -> Self::State
State for this WorldQuery type.Source§fn matches_component_set(
state: &Self::State,
set_contains_id: &impl Fn(ComponentId) -> bool,
) -> bool
fn matches_component_set( state: &Self::State, set_contains_id: &impl Fn(ComponentId) -> bool, ) -> bool
Source§fn set_access(_state: &mut Self::State, _access: &FilteredAccess<ComponentId>)
fn set_access(_state: &mut Self::State, _access: &FilteredAccess<ComponentId>)
FilteredEntityRef
or FilteredEntityMut. Read moreimpl<T: Kind> Copy for Instance<T>
impl<T: Kind> Eq for Instance<T>
impl<T: Kind> ReadOnlyQueryData for Instance<T>
Auto Trait Implementations§
impl<T> Freeze for Instance<T>
impl<T> RefUnwindSafe for Instance<T>where
T: RefUnwindSafe,
impl<T> Send for Instance<T>
impl<T> Sync for Instance<T>
impl<T> Unpin for Instance<T>where
T: Unpin,
impl<T> UnwindSafe for Instance<T>where
T: UnwindSafe,
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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSend for T
Source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
Source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path.Source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
Source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident.Source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name.Source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
Source§impl<T> DynamicTyped for Twhere
T: Typed,
impl<T> DynamicTyped for Twhere
T: Typed,
Source§fn reflect_type_info(&self) -> &'static TypeInfo
fn reflect_type_info(&self) -> &'static TypeInfo
Typed::type_info.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> GetPath for T
impl<T> GetPath for T
Source§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&(dyn PartialReflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p>, ) -> Result<&(dyn PartialReflect + 'static), ReflectPathError<'p>>
path. Read moreSource§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut (dyn PartialReflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut (dyn PartialReflect + 'static), ReflectPathError<'p>>
path. Read moreSource§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path. Read moreSource§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path. Read more