#asset #assets #cache #resources

assets_manager

Conveniently load, cache, and reload external resources

10 releases

✓ Uses Rust 2018 edition

new 0.3.2 Jul 5, 2020
0.3.1 Jun 23, 2020
0.2.2 Apr 29, 2020
0.1.3 Mar 23, 2020

#13 in Caching

Download history 119/week @ 2020-03-15 117/week @ 2020-03-22 74/week @ 2020-03-29 68/week @ 2020-04-05 38/week @ 2020-04-12 6/week @ 2020-04-19 10/week @ 2020-04-26 19/week @ 2020-05-03 26/week @ 2020-05-10 28/week @ 2020-05-17 32/week @ 2020-05-24 31/week @ 2020-05-31 23/week @ 2020-06-07 23/week @ 2020-06-14 27/week @ 2020-06-21 14/week @ 2020-06-28

107 downloads per month

MIT/Apache

79KB
1.5K SLoC

Assets-manager

Crates.io Docs.rs Minimum rustc version

This crate aims at providing a filesystem abstraction to easily load external resources. It was originally thought for games, but can of course be used in other contexts.

Original idea was inspired by Veloren's assets system.

This crate follow semver convention and supports rustc 1.42.0 and higher. Changing this is considered a breaking change.

Goals

This crates focuses on:

  • Good perfomances:
    Crucial for perfomance-oriented applications such as games.
    Loaded assets are cached so loading one several times is as fast as loading it once. This crate was thought for use with concurrency.

  • Hot-reloading:
    Hot-reloading means updating assets in memory as soon as the corresponding file is changed, without restarting your program. It may greatly ease development.
    Your time is precious, and first-class support of hot-reloading helps you saving it.

  • Pleasant to use:
    A well-documented high-level API, easy to learn.
    Built-in support of common formats with serde.\

  • Lightness:
    Pay for what you take, no dependency bloat.

Example

Suppose that you have a file assets/common/position.ron containing this:

Point(
    x: 5,
    y: -6,
)

Then you can load it this way (with feature ron enabled):

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

// The struct you want to load
#[derive(Deserialize)]
struct Point {
    x: i32,
    y: i32,
}

// Specify how you want the structure to be loaded
impl Asset for Point {
    // The extension of the files to look into
    const EXTENSION: &'static str = "ron";

    // The serialization format (RON)
    type Loader = loader::RonLoader;
}


// Create a new cache to load assets under the "./assets" folder
let cache = AssetCache::new("assets")?;

// Get a lock on the asset
// This will load the file `./assets/common/position.ron`
let asset_lock = cache.load::<Point>("common.position")?;

// Lock the asset for reading
// Any number of read locks can exist at the same time,
// but none can exist when the asset is reloaded
let point = asset_lock.read();

// The asset is now ready to be used
assert_eq!(point.x, 5);
assert_eq!(point.y, -6);

// Loading the same asset retreives it from the cache
let other_lock = cache.load("common.position")?;
assert!(asset_lock.ptr_eq(&other_lock));

Hot-reloading is also very easy to use:

let cache = AssetCache::new("assets")?;
let asset_lock = cache.load::<Point>("common.position")?;

loop {
    // Reload all cached files that changed
    cache.hot_reload();

    // Assets are updated without any further work
    println!("Current value: {:?}", asset_lock.read());
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~91–730KB
~15K SLoC