Trait assets_manager::asset::Asset
source · pub trait Asset: Sized + Send + Sync + 'static {
type Loader: Loader<Self>;
const EXTENSION: &'static str = "";
const EXTENSIONS: &'static [&'static str] = _;
const HOT_RELOADED: bool = true;
// Provided method
fn default_value(
id: &SharedString,
error: BoxedError
) -> Result<Self, BoxedError> { ... }
}
Expand description
An asset is a type loadable from raw bytes.
Asset
s can be loaded and retrieved by an AssetCache
.
This trait should only perform a conversion from raw bytes to the concrete
type. If you need to load other assets, please use the Compound
trait.
§Extension
You can provide several extensions that will be used to search and load
assets. When loaded, each extension is tried in order until a file is
correctly loaded or no extension remains. The empty string ""
means a file
without extension. You cannot use character .
.
The EXTENSION
field is a convenient shortcut if your asset uses only one
extension. If you set a value for EXTENSIONS
too, this field is ignored.
If neither EXTENSION
nor EXTENSIONS
is set, the default is no extension.
If you use hot-reloading, the asset will be reloaded each time one of the file with the given extension is touched.
§Example
Suppose you make a physics simulation, and you store positions and speeds in a Bincode-encoded file, with extension “.data”.
use assets_manager::{Asset, loader};
use serde::Deserialize;
#[derive(Deserialize)]
struct Vector {
x: f32,
y: f32,
z: f32,
}
#[derive(Deserialize)]
struct World {
pos: Vec<Vector>,
speed: Vec<Vector>,
}
impl Asset for World {
const EXTENSION: &'static str = "data";
type Loader = loader::BincodeLoader;
}
Required Associated Types§
Provided Associated Constants§
sourceconst EXTENSION: &'static str = ""
const EXTENSION: &'static str = ""
Use this field if your asset only uses one extension.
This value is ignored if you set EXTENSIONS
too.
sourceconst EXTENSIONS: &'static [&'static str] = _
const EXTENSIONS: &'static [&'static str] = _
This field enables you to specify multiple extension for an asset.
If EXTENSION
is provided, you don’t have to set this constant.
If this array is empty, loading an asset of this type returns an error
unless a default value is provided with the default_value
method.
sourceconst HOT_RELOADED: bool = true
const HOT_RELOADED: bool = true
If false
, disable hot-reloading for assets of this type (true
by
default). If so, you may want to implement NotHotReloaded
for this
type to enable additional functions.
Provided Methods§
sourcefn default_value(
id: &SharedString,
error: BoxedError
) -> Result<Self, BoxedError>
fn default_value( id: &SharedString, error: BoxedError ) -> Result<Self, BoxedError>
Specifies a eventual default value to use if an asset fails to load. If
this method returns Ok
, the returned value is used as an asset. In
particular, if this method always returns Ok
, AssetCache::load
is
guaranteed not to fail.
The id
parameter is given to easily report the error.
By default, this method always returns an error.
§Example
On error, log it and return a default value:
use assets_manager::{Asset, BoxedError, SharedString, loader};
use serde::Deserialize;
#[derive(Deserialize, Default)]
struct Item {
name: String,
kind: String,
}
impl Asset for Item {
const EXTENSION: &'static str = "json";
type Loader = loader::JsonLoader;
fn default_value(id: &SharedString, error: BoxedError) -> Result<Item, BoxedError> {
eprintln!("Error loading {}: {}. Using default value", id, error);
Ok(Item::default())
}
}
Object Safety§
Implementations on Foreign Types§
source§impl Asset for FontArc
Available on crate feature ab_glyph
only.
impl Asset for FontArc
ab_glyph
only.type Loader = FontLoader
const EXTENSIONS: &'static [&'static str] = _
source§impl Asset for FontVec
Available on crate feature ab_glyph
only.
impl Asset for FontVec
ab_glyph
only.type Loader = FontLoader
const EXTENSIONS: &'static [&'static str] = _
source§impl Asset for Gltf
Available on crate feature gltf
only.
impl Asset for Gltf
gltf
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = GltfLoader
Implementors§
source§impl Asset for Bmp
Available on crate feature bmp
only.
impl Asset for Bmp
bmp
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = ImageLoader
source§impl Asset for Flac
Available on crate feature flac
only.
impl Asset for Flac
flac
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = SoundLoader
source§impl Asset for Jpeg
Available on crate feature jpeg
only.
impl Asset for Jpeg
jpeg
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = ImageLoader
source§impl Asset for Mp3
Available on crate feature mp3
only.
impl Asset for Mp3
mp3
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = SoundLoader
source§impl Asset for Png
Available on crate feature png
only.
impl Asset for Png
png
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = ImageLoader
source§impl Asset for Vorbis
Available on crate feature vorbis
only.
impl Asset for Vorbis
vorbis
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = SoundLoader
source§impl Asset for Wav
Available on crate feature wav
only.
impl Asset for Wav
wav
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = SoundLoader
source§impl Asset for Webp
Available on crate feature webp
only.
impl Asset for Webp
webp
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = ImageLoader
source§impl<T> Asset for Json<T>
Available on crate feature json
only.
impl<T> Asset for Json<T>
json
only.const EXTENSIONS: &'static [&'static str] = _
type Loader = LoadFrom<T, JsonLoader>
source§impl<T> Asset for Toml<T>
Available on crate feature toml
only.
impl<T> Asset for Toml<T>
toml
only.