[][src]Struct assets_manager::AssetCache

pub struct AssetCache<'a> { /* fields omitted */ }

The main structure of this crate, used to cache assets.

It uses interior mutability, so assets can be added in the cache without requiring a mutable reference, but one is required to remove an asset.

Within the cache, assets are identified with their type and a string. This string is constructed from the asset path, remplacing / by . and removing the extension.

Example

use assets_manager::{Asset, AssetCache, loader};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Point {
    x: i32,
    y: i32,
}

impl Asset for Point {
    const EXT: &'static str = "ron";
    type Loader = loader::RonLoader;
}

// Create a cache
let cache = AssetCache::new("assets");

// Get an asset from the file `assets/common/position.ron`
let point_lock = cache.load::<Point>("common.position")?;

// Read it
let point = point_lock.read();
println!("Loaded position: {:?}", point);

// Drop the guard to avoid a deadlock
drop(point);

// Reload the asset from the filesystem
cache.reload::<Point>("common.position")?;
println!("New position: {:?}", point_lock.read());

Methods

impl<'a> AssetCache<'a>[src]

pub fn new(path: &str) -> AssetCache[src]

Creates a new cache.

Assets will be searched in the directory path

pub fn load<A: Asset>(&self, id: &str) -> Result<AssetRefLock<A>, AssetError>[src]

Loads an asset.

If the asset is not found in the cache, it is loaded from the filesystem.

Errors

Errors can occur in several cases :

  • The asset could not be loaded from the filesystem
  • Loaded data could not not be converted properly

pub fn load_cached<A: Asset>(&self, id: &str) -> Option<AssetRefLock<A>>[src]

Loads an asset from the cache.

This function does not attempt to load the asset from the filesystem if it is not found in the cache.

pub fn load_expect<A: Asset>(&self, id: &str) -> AssetRefLock<A>[src]

Loads an asset given an id, from the filesystem or the cache.

Panics

Panics if an error happens while loading the asset (see load).

pub fn reload<A: Asset>(&self, id: &str) -> Result<AssetRefLock<A>, AssetError>[src]

Reloads an asset from the filesystem.

It does not matter whether the asset has been loaded yet.

Note: this function requires a write lock on the asset, and will block until one is aquired, ie no read lock can exist at the same time. This means that you MUST NOT call this method if you have an AssetRef on the same asset, or it may cause a deadlock.

Errors

Error cases are the same as load.

If an error occurs, the asset is left unmodified.

pub fn remove<A: Asset>(&mut self, id: &str)[src]

Remove an asset from the cache.

The removed asset matches both the id and the type parameter.

pub fn take<A: Asset>(&mut self, id: &str) -> Option<A>[src]

Take ownership on an asset.

The corresponding asset is removed from the cache.

pub fn clear(&mut self)[src]

Clears the cache.

Trait Implementations

impl<'_> Debug for AssetCache<'_>[src]

Auto Trait Implementations

impl<'a> !RefUnwindSafe for AssetCache<'a>

impl<'a> Send for AssetCache<'a>

impl<'a> Sync for AssetCache<'a>

impl<'a> Unpin for AssetCache<'a>

impl<'a> !UnwindSafe for AssetCache<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.