Expand description
Conveniently load, store and cache external resources.
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.
The structure AssetCache
is the entry point of the crate. See Asset
documentation
to see how to define custom asset types.
§Cargo features
hot-reloading
: Add hot-reloading.macros
: Add support for derivingAsset
trait.
§Additional sources
Enable reading assets from sources other than the filesystem.
These sources are defined in the source
module:
embedded
: Embeds asset files directly in your binary at compile timezip
: Reads assets from ZIP archives- Optional compression:
zip-deflate
,zip-zstd
- Optional compression:
tar
: Reads assets from TAR archives
§Additional formats
These features add support for various asset formats:
- Serialisation formats (using
serde
):bincode
,json
,msgpack
,ron
,toml
,yaml
. - Image formats (using
image
):bmp
,jpeg
,png
webp
. - GlTF format (using
gltf
):gltf
.
§External crates support
Support of some other crates is done in external crates:
§Internal features
These features change internal data structures implementations.
parking_lot
: Useparking_lot
’s synchronization primitives.faster-hash
: Use a faster hashing algorithm (enabled by default).
§Basic example
Given a file assets/common/position.ron
which contains this:
Point(
x: 5,
y: -6,
)
You can load and use it as follows:
use assets_manager::{BoxedError, AssetCache, FileAsset};
use serde::Deserialize;
use std::borrow::Cow;
// The struct you want to load
#[derive(Deserialize)]
struct Point {
x: i32,
y: i32,
}
// Specify how you want the structure to be loaded
impl FileAsset for Point {
// The extension of the files to look into
const EXTENSION: &'static str = "ron";
// The serialization format
//
// In this specific case, the derive macro could be used but we use the
// full version as a demo.
fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Result<Self, BoxedError> {
assets_manager::asset::load_ron(&bytes)
}
}
// Create a new cache to load assets under the "./assets" folder
let cache = AssetCache::new("assets")?;
// Get a handle on the asset
// This will load the file `./assets/common/position.ron`
let handle = 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 = handle.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_handle = cache.load("common.position")?;
assert!(std::ptr::eq(handle, other_handle));
§Hot-reloading
Hot-reloading is a major feature of assets_manager
: when a file is added,
modified or deleted, the values of all assets that depend on this file are
automatically and transparently updated. It is managed automatically in the
background.
See the asset
module for a precise description of how assets interact
with hot-reloading.
Re-exports§
pub use asset::Asset;
pub use asset::Compound;
Deprecated pub use asset::FileAsset;
pub use asset::Storable;
Modules§
- asset
- Values loadable from a cache.
- hot_
reloading - Tools to implement hot-reloading.
- source
- Bytes sources to load assets from.
Structs§
- ArcHandle
- A strong pointer to a handle.
- Asset
Cache - The main structure of this crate, used to load and store assets.
- Asset
Read Guard - RAII guard used to keep a read lock on an asset and release it when dropped.
- Atomic
Reload Id - A
ReloadId
that can be shared between threads. - Directory
- Stores ids in a directory containing assets of type
T
- Error
- The error type which is used when loading an asset.
- Handle
- A handle on an asset.
- Once
Init Cell utils
- A thread-safe cell which can be written to only once.
- RawDirectory
- Stores ids in a directory containing assets of type
T
- RawRecursive
Directory - Stores ids in a recursive directory containing assets of type
T
- Recursive
Directory - Stores ids in a recursive directory containing assets of type
T
- Reload
Id - An id to know when an asset is reloaded.
- Reload
Watcher - A watcher that can tell when an asset is reloaded.
- Shared
Bytes - Bytes that can easily be shared.
- Shared
String - A string that can easily be shared.
- Weak
Handle - A weak pointer to a handle.
Type Aliases§
- ArcUntyped
Handle - An untyped version of
ArcHandle
. - Boxed
Error - A boxed error
- Untyped
Handle - A untyped handle on an asset.
- Weak
Untyped Handle - An untyped version of
WeakHandle
.