Struct assets_manager::Handle
source · pub struct Handle<'a, T> { /* private fields */ }Expand description
A handle on an asset.
Such a handle can be used to get access to an asset of type A. It is
generally obtained by call AssetCache::load and its variants.
If feature hot-reloading is used, this structure wraps 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 (when
calling AssetCache::hot_reload).
This is the structure you want to use to store a reference to an asset.
However it is generally easier to work with 'static data. For more
information, see top-level documentation.
Implementations§
source§impl<'a, T> Handle<'a, T>
impl<'a, T> Handle<'a, T>
sourcepub fn read(&self) -> AssetGuard<'a, T>
pub fn read(&self) -> AssetGuard<'a, T>
Locks the pointed asset for reading.
If T implements NotHotReloaded or if hot-reloading is disabled, no
reloading can occur so there is no actual lock. In these cases, calling
this function is cheap and do not involve synchronisation.
Returns a RAII guard which will release the lock once dropped.
sourcepub fn id(&self) -> &'a SharedString
pub fn id(&self) -> &'a SharedString
Returns the id of the asset.
Note that the lifetime of the returned &str is tied to that of the
AssetCache, so it can outlive the handle.
sourcepub fn reload_watcher(&self) -> ReloadWatcher<'a>
pub fn reload_watcher(&self) -> ReloadWatcher<'a>
Returns a ReloadWatcher that can be used to check whether this asset
was reloaded.
Example
use assets_manager::{Asset, 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 {
cache.hot_reload();
if watcher.reloaded() {
println!("The asset was reloaded !")
}
// Calling `reloaded` once more returns false: the asset has not
// been reloaded since last call to `reloaded`
assert!(!watcher.reloaded());
}
sourcepub fn last_reload_id(&self) -> ReloadId
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.
sourcepub fn reloaded_global(&self) -> bool
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.
sourcepub fn same_handle(&self, other: &Self) -> bool
pub fn same_handle(&self, other: &Self) -> bool
Checks if the two handles refer to the same asset.
source§impl<'a, A> Handle<'a, A>where
A: NotHotReloaded,
impl<'a, A> Handle<'a, A>where
A: NotHotReloaded,
Trait Implementations§
source§impl<T, U> PartialEq<Handle<'_, U>> for Handle<'_, T>where
T: PartialEq<U>,
impl<T, U> PartialEq<Handle<'_, U>> for Handle<'_, T>where
T: PartialEq<U>,
impl<A> Copy for Handle<'_, A>
impl<A> Eq for Handle<'_, A>where
A: Eq,
Auto Trait Implementations§
impl<'a, T> !RefUnwindSafe for Handle<'a, T>
impl<'a, T> Send for Handle<'a, T>where
T: Send + Sync,
impl<'a, T> Sync for Handle<'a, T>where
T: Send + Sync,
impl<'a, T> Unpin for Handle<'a, T>
impl<'a, T> !UnwindSafe for Handle<'a, T>
Blanket Implementations§
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.