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.

Assets 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§

source

type Loader: Loader<Self>

Specifies a way to convert raw bytes into the asset.

See module loader for implementations of common conversions.

Provided Associated Constants§

source

const EXTENSION: &'static str = ""

Use this field if your asset only uses one extension.

This value is ignored if you set EXTENSIONS too.

source

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.

source

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§

source

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§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Asset for Box<str>

source§

const EXTENSION: &'static str = "txt"

§

type Loader = StringLoader

source§

impl Asset for String

source§

const EXTENSION: &'static str = "txt"

§

type Loader = StringLoader

source§

impl Asset for FontArc

Available on crate feature ab_glyph only.
§

type Loader = FontLoader

source§

const EXTENSIONS: &'static [&'static str] = _

source§

impl Asset for FontVec

Available on crate feature ab_glyph only.
§

type Loader = FontLoader

source§

const EXTENSIONS: &'static [&'static str] = _

source§

impl Asset for Gltf

Available on crate feature gltf only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = GltfLoader

Implementors§

source§

impl Asset for SharedString

source§

const EXTENSION: &'static str = "txt"

§

type Loader = StringLoader

source§

impl Asset for Bmp

Available on crate feature bmp only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = ImageLoader

source§

impl Asset for Flac

Available on crate feature flac only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = SoundLoader

source§

impl Asset for Jpeg

Available on crate feature jpeg only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = ImageLoader

source§

impl Asset for Mp3

Available on crate feature mp3 only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = SoundLoader

source§

impl Asset for Png

Available on crate feature png only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = ImageLoader

source§

impl Asset for Vorbis

Available on crate feature vorbis only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = SoundLoader

source§

impl Asset for Wav

Available on crate feature wav only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = SoundLoader

source§

impl Asset for Webp

Available on crate feature webp only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = ImageLoader

source§

impl<T> Asset for Json<T>
where T: for<'de> Deserialize<'de> + Send + Sync + 'static,

Available on crate feature json only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = LoadFrom<T, JsonLoader>

source§

impl<T> Asset for Ron<T>
where T: for<'de> Deserialize<'de> + Send + Sync + 'static,

Available on crate feature ron only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = LoadFrom<T, RonLoader>

source§

impl<T> Asset for Toml<T>
where T: for<'de> Deserialize<'de> + Send + Sync + 'static,

Available on crate feature toml only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = LoadFrom<T, TomlLoader>

source§

impl<T> Asset for Yaml<T>
where T: for<'de> Deserialize<'de> + Send + Sync + 'static,

Available on crate feature yaml only.
source§

const EXTENSIONS: &'static [&'static str] = _

§

type Loader = LoadFrom<T, YamlLoader>