Handle

Struct Handle 

Source
pub struct Handle<T: ?Sized> { /* private fields */ }
Expand description

A handle on an asset.

It can be obtained through an AssetCache.

If feature hot-reloading is used, this structure may wrap a RwLock, so assets can be written to be reloaded. As such, any number of read guard can exist at the same time, but none can exist while reloading an asset.

You can use this structure to reference an asset directly. However you can only get references to this type, never own it. If you don’t want to deal with lifetimes, you can:

  • Get a &'static AssetCache (eg with a static LazyLock), to get a 'static Handle refernce.
  • Store the id of the asset and get it from the cache as needed.
  • Get a ArcHandle through strong method.

Implementations§

Source§

impl Handle<dyn Any + Sync + Send>

Source

pub fn type_id(&self) -> TypeId

Gets the TypeId of the underlying type.

Source

pub fn is<T: 'static>(&self) -> bool

Returns true if the inner type is the same as T.

Source

pub fn downcast_ref<T: Storable>(&self) -> Option<&Handle<T>>

Returns a handle to the asset if it is of type T.

Source§

impl<T: ?Sized> Handle<T>

Source

pub fn read(&self) -> AssetReadGuard<'_, T>

Locks the pointed asset for reading.

If hot-reloading is disabled for T or globally, no reloading can occur so there is no actual lock. In these cases, calling this function does not involve synchronisation.

Returns a RAII guard which will release the lock once dropped.

Source

pub fn id(&self) -> &SharedString

Returns the id of the asset.

Source

pub fn as_untyped(&self) -> &UntypedHandle
where T: Storable,

Returns an untyped version of the handle.

Source

pub fn strong(&self) -> ArcHandle<T>

Make a ArcHandle that points to this handle.

Source

pub fn weak(&self) -> WeakHandle<T>

Make a WeakHandle that points to this handle.

Source

pub fn strong_count(&self) -> usize

Gets the number of strong (ArcHandle) pointers to this allocation.

Source

pub fn weak_count(&self) -> usize

Gets the number of WeakHandle pointers to this allocation.

Source

pub fn reload_watcher(&self) -> ReloadWatcher<'_>

Returns a ReloadWatcher that can be used to check whether this asset was reloaded.

§Example
use assets_manager::{AssetCache, ReloadWatcher};

let cache = AssetCache::new("assets")?;
let asset = cache.load::<String>("common.some_text")?;
let mut watcher = asset.reload_watcher();

// The handle has just been created, so `reloaded` returns false
assert!(!watcher.reloaded());

loop {
    if watcher.reloaded() {
        println!("The asset was reloaded !")
    }
}
Source

pub fn last_reload_id(&self) -> ReloadId

Returns the last ReloadId associated with this asset.

It is only meaningful when compared to other ReloadIds returned by the same handle or to ReloadId::NEVER.

Source

pub fn reloaded_global(&self) -> bool

Returns true if the asset has been reloaded since last call to this method with any handle on this asset.

Note that this method and reload_watcher are totally independant, and the result of the two functions do not depend on whether the other was called.

Source§

impl<T> Handle<T>
where T: Copy,

Source

pub fn copied(&self) -> T

Returns a copy of the inner asset.

This is functionnally equivalent to cloned, but it ensures that no expensive operation is used (eg if a type is refactored).

Source§

impl<T> Handle<T>
where T: Clone,

Source

pub fn cloned(&self) -> T

Returns a clone of the inner asset.

Trait Implementations§

Source§

impl<T> Debug for Handle<T>
where T: Debug + ?Sized,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Sync + ?Sized> Sync for Handle<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Handle<T>

§

impl<T> !RefUnwindSafe for Handle<T>

§

impl<T> Send for Handle<T>
where T: Send + ?Sized,

§

impl<T> Unpin for Handle<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for Handle<T>
where T: UnwindSafe + ?Sized,

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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> Storable for T
where T: Send + Sync + 'static,