Trait veloren_common_assets::Asset

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: Box<dyn Error + Send + Sync>,
    ) -> Result<Self, Box<dyn Error + Send + Sync>> { ... }
}
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§

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§

const EXTENSION: &'static str = ""

Use this field if your asset only uses one extension.

This value is ignored if you set EXTENSIONS too.

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.

const HOT_RELOADED: bool = true

If false, disable hot-reloading for assets of this type (true by default). This avoids having to lock the asset to read it (ie it makes [Handle::read] a noop)

Provided Methods§

fn default_value( id: &SharedString, error: Box<dyn Error + Send + Sync>, ) -> Result<Self, Box<dyn Error + Send + Sync>>

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§

§

impl Asset for Box<str>

§

const EXTENSION: &'static str = "txt"

§

type Loader = StringLoader

§

impl Asset for String

§

const EXTENSION: &'static str = "txt"

§

type Loader = StringLoader

§

impl Asset for FontArc

§

type Loader = FontLoader

§

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

§

impl Asset for FontVec

§

type Loader = FontLoader

§

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

Implementors§

source§

impl Asset for DotVoxAsset

§

type Loader = DotVoxLoader

source§

const EXTENSION: &'static str = "vox"

source§

impl Asset for Image

§

type Loader = ImageLoader

source§

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

source§

impl Asset for ObjAsset

§

type Loader = ObjAssetLoader

source§

const EXTENSION: &'static str = "obj"

§

impl Asset for SharedString

§

const EXTENSION: &'static str = "txt"

§

type Loader = StringLoader

§

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

§

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

§

type Loader = LoadFrom<T, RonLoader>

§

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

§

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

§

type Loader = LoadFrom<T, JsonLoader>