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>

source

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.

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

source

pub fn get(&self) -> &T

Returns a reference to the underlying asset.

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

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,

source§

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

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

impl<T> Serialize for Handle<T>
where T: Serialize,

Available on crate feature serde only.
source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Handle<T>

§

impl<T> !RefUnwindSafe for Handle<T>

§

impl<T> Send for Handle<T>
where T: Send,

§

impl<T> Sync for Handle<T>
where T: Sync,

§

impl<T> Unpin for Handle<T>
where T: Unpin,

§

impl<T> UnwindSafe for Handle<T>
where T: UnwindSafe,

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<S> FromSample<S> for S

source§

fn from_sample_(s: S) -> S

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> ToSample<U> for T
where U: FromSample<T>,

source§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,