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§

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.

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.

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());
}

Returns the last ReloadId associated with this asset.

It is only meaningful when compared to other ReloadIds returned by the same handle.

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.

Checks if the two handles refer to the same asset.

Returns a reference to the underlying asset.

This method only works if hot-reloading is disabled for the given type.

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).

Returns a clone of the inner asset.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.