Struct bevy_asset::Handle
source · pub struct Handle<T>where
T: Asset,{ /* private fields */ }
Expand description
A handle into a specific Asset
of type T
.
Handles contain a unique id that corresponds to a specific asset in the Assets
collection.
Accessing the Asset
A handle is not the asset itself, but should be seen as a pointer to the asset. Modifying a
handle’s id
only modifies which asset is being pointed to. To get the actual asset, try using
Assets::get
or Assets::get_mut
.
Strong and Weak
A handle can be either “Strong” or “Weak”. Simply put: Strong handles keep the asset loaded, while Weak handles do not affect the loaded status of assets. This is due to a type of reference counting. When the number of Strong handles that exist for any given asset reach zero, the asset is dropped and becomes unloaded. In some cases, you might want a reference to an asset but don’t want to take the responsibility of keeping it loaded that comes with a Strong handle. This is where a Weak handle can be very useful.
For example, imagine you have a Sprite
component and a Collider
component. The Collider
uses
the Sprite
’s image size to check for collisions. It does so by keeping a Weak copy of the
Sprite
’s Strong handle to the image asset.
If the Sprite
is removed, its Strong handle to the image is dropped with it. And since it was the
only Strong handle for that asset, the asset is unloaded. Our Collider
component still has a Weak
handle to the unloaded asset, but it will not be able to retrieve the image data, resulting in
collisions no longer being detected for that entity.
Implementations§
source§impl<T: Asset> Handle<T>
impl<T: Asset> Handle<T>
sourcepub fn id(&self) -> HandleId
pub fn id(&self) -> HandleId
The ID of the asset as contained within its respective Assets
collection.
sourcepub fn cast_weak<U: Asset>(&self) -> Handle<U>
pub fn cast_weak<U: Asset>(&self) -> Handle<U>
Recasts this handle as a weak handle of an Asset U
.
sourcepub fn make_strong(&mut self, assets: &Assets<T>)
pub fn make_strong(&mut self, assets: &Assets<T>)
Makes this handle Strong if it wasn’t already.
This method requires the corresponding Assets
collection.
sourcepub fn clone_weak(&self) -> Self
pub fn clone_weak(&self) -> Self
Creates a weak copy of this handle.
sourcepub fn clone_untyped(&self) -> HandleUntyped
pub fn clone_untyped(&self) -> HandleUntyped
Creates an untyped copy of this handle.
sourcepub fn clone_weak_untyped(&self) -> HandleUntyped
pub fn clone_weak_untyped(&self) -> HandleUntyped
Creates a weak, untyped copy of this handle.
Trait Implementations§
source§impl<T> Component for Handle<T>where
T: Asset,
Self: Send + Sync + 'static,
impl<T> Component for Handle<T>where T: Asset, Self: Send + Sync + 'static,
type Storage = TableStorage
source§impl<T> FromReflect for Handle<T>where
T: Asset,
HandleId: FromReflect,
impl<T> FromReflect for Handle<T>where T: Asset, HandleId: FromReflect,
source§fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
Self
from a reflected value.source§impl<T> GetTypeRegistration for Handle<T>where
T: Asset,
HandleId: Reflect,
HandleType: Any + Send + Sync,
PhantomData<fn() -> T>: Any + Send + Sync,
impl<T> GetTypeRegistration for Handle<T>where T: Asset, HandleId: Reflect, HandleType: Any + Send + Sync, PhantomData<fn() -> T>: Any + Send + Sync,
source§impl<T: Asset> Ord for Handle<T>
impl<T: Asset> Ord for Handle<T>
source§impl<T: Asset> PartialEq<Handle<T>> for Handle<T>
impl<T: Asset> PartialEq<Handle<T>> for Handle<T>
source§impl<T: Asset> PartialOrd<Handle<T>> for Handle<T>
impl<T: Asset> PartialOrd<Handle<T>> for Handle<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<T> Reflect for Handle<T>where
T: Asset,
HandleId: Reflect,
HandleType: Any + Send + Sync,
PhantomData<fn() -> T>: Any + Send + Sync,
impl<T> Reflect for Handle<T>where T: Asset, HandleId: Reflect, HandleType: Any + Send + Sync, PhantomData<fn() -> T>: Any + Send + Sync,
source§fn get_type_info(&self) -> &'static TypeInfo
fn get_type_info(&self) -> &'static TypeInfo
source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
&mut dyn Any
.source§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§fn clone_value(&self) -> Box<dyn Reflect>
fn clone_value(&self) -> Box<dyn Reflect>
Reflect
trait object. Read moresource§fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
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 reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
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 serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
source§impl<T> Struct for Handle<T>where
T: Asset,
HandleId: Reflect,
HandleType: Any + Send + Sync,
PhantomData<fn() -> T>: Any + Send + Sync,
impl<T> Struct for Handle<T>where T: Asset, HandleId: Reflect, HandleType: Any + Send + Sync, PhantomData<fn() -> T>: Any + Send + Sync,
source§fn field(&self, name: &str) -> Option<&dyn Reflect>
fn field(&self, name: &str) -> Option<&dyn Reflect>
name
as a &dyn Reflect
.source§fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>
fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>
name
as a
&mut dyn Reflect
.source§fn field_at(&self, index: usize) -> Option<&dyn Reflect>
fn field_at(&self, index: usize) -> Option<&dyn Reflect>
index
as a
&dyn Reflect
.source§fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>
fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>
index
as a &mut dyn Reflect
.source§fn name_at(&self, index: usize) -> Option<&str>
fn name_at(&self, index: usize) -> Option<&str>
index
.source§fn iter_fields(&self) -> FieldIter<'_>
fn iter_fields(&self) -> FieldIter<'_>
source§fn clone_dynamic(&self) -> DynamicStruct
fn clone_dynamic(&self) -> DynamicStruct
DynamicStruct
.source§impl<T> Typed for Handle<T>where
T: Asset,
HandleId: Reflect,
HandleType: Any + Send + Sync,
PhantomData<fn() -> T>: Any + Send + Sync,
impl<T> Typed for Handle<T>where T: Asset, HandleId: Reflect, HandleType: Any + Send + Sync, PhantomData<fn() -> T>: Any + Send + Sync,
impl<T: Asset> Eq for Handle<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Handle<T>
impl<T> Send for Handle<T>
impl<T> Sync for Handle<T>
impl<T> Unpin for Handle<T>
impl<T> UnwindSafe for Handle<T>
Blanket Implementations§
source§impl<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere C: Component,
fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId) )
unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> Cwhere F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a, Aligned>,
fn get_components( self, func: &mut impl FnMut(StorageType, OwningPtr<'_, Aligned>) )
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
source§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
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<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere T: Default,
source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given Worldsource§impl<S> GetField for Swhere
S: Struct,
impl<S> GetField for Swhere S: Struct,
source§impl<T> GetPath for Twhere
T: Reflect,
impl<T> GetPath for Twhere T: Reflect,
source§fn reflect_path<'r, 'p>(
&'r self,
path: &'p str
) -> Result<&'r (dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path<'r, 'p>( &'r self, path: &'p str ) -> Result<&'r (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moresource§fn reflect_path_mut<'r, 'p>(
&'r mut self,
path: &'p str
) -> Result<&'r mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'r, 'p>( &'r mut self, path: &'p str ) -> Result<&'r mut (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read more