Struct assets_manager::Handle
source · pub struct Handle<T> { /* private fields */ }
Expand description
A handle on an asset.
Such a handle can be used to get access to an asset of type T
. 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
).
You can use thus structure 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<T> Handle<T>
impl<T> Handle<T>
sourcepub fn read(&self) -> AssetReadGuard<'_, T>
pub fn read(&self) -> AssetReadGuard<'_, 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 does not involve synchronisation.
Returns a RAII guard which will release the lock once dropped.
sourcepub fn id(&self) -> &SharedString
pub fn id(&self) -> &SharedString
Returns the id of the asset.
sourcepub fn as_untyped(&self) -> &UntypedHandlewhere
T: Storable,
pub fn as_untyped(&self) -> &UntypedHandlewhere
T: Storable,
Returns an untyped version of the handle.
sourcepub fn reload_watcher(&self) -> ReloadWatcher<'_>
pub fn reload_watcher(&self) -> ReloadWatcher<'_>
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 ReloadId
s returned by the
same handle or to ReloadId::NEVER
.
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.