Trait FileAsset

pub trait FileAsset: Storable {
    const EXTENSION: &'static str = "";
    const EXTENSIONS: &'static [&'static str] = _;
    const HOT_RELOADED: bool = true;

    // Required method
    fn from_bytes(
        bytes: Cow<'_, [u8]>,
    ) -> Result<Self, Box<dyn Error + Sync + Send>>;

    // Provided method
    fn default_value(
        id: &SharedString,
        error: Box<dyn Error + Sync + Send>,
    ) -> Result<Self, Box<dyn Error + Sync + Send>> { ... }
}
Expand description

An asset that can be loaded from a single file.

Implementing this trait provides an implementation of Asset.

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, disables hot-reloading of assets of this type (true by default).

Required Methods§

fn from_bytes( bytes: Cow<'_, [u8]>, ) -> Result<Self, Box<dyn Error + Sync + Send>>

Creates a value of this type from raw bytes.

Provided Methods§

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

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::{BoxedError, FileAsset, SharedString};
use std::borrow::Cow;

#[derive(serde::Deserialize, Default)]
struct Item {
    name: String,
    kind: String,
}

impl FileAsset for Item {
    const EXTENSION: &'static str = "json";

    fn from_bytes(bytes: Cow<[u8]>) -> Result<Self, BoxedError> {
        assets_manager::asset::load_json(&bytes)
    }

    fn default_value(id: &SharedString, error: BoxedError) -> Result<Item, BoxedError> {
        log::warn!("Error loading {}: {}. Using default value", id, error);
        Ok(Item::default())
    }
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

§

impl FileAsset for Box<str>

§

const EXTENSION: &'static str = "txt"

§

fn from_bytes( bytes: Cow<'_, [u8]>, ) -> Result<Box<str>, Box<dyn Error + Sync + Send>>

§

impl FileAsset for String

§

const EXTENSION: &'static str = "txt"

§

fn from_bytes( bytes: Cow<'_, [u8]>, ) -> Result<String, Box<dyn Error + Sync + Send>>

§

impl FileAsset for Arc<str>

§

const EXTENSION: &'static str = "txt"

§

fn from_bytes( bytes: Cow<'_, [u8]>, ) -> Result<Arc<str>, Box<dyn Error + Sync + Send>>

§

impl FileAsset for FontArc

§

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

§

fn from_bytes( bytes: Cow<'_, [u8]>, ) -> Result<FontArc, Box<dyn Error + Sync + Send>>

§

impl FileAsset for FontVec

§

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

§

fn from_bytes( bytes: Cow<'_, [u8]>, ) -> Result<FontVec, Box<dyn Error + Sync + Send>>

Implementors§

Source§

impl FileAsset for DotVox

Source§

const EXTENSION: &'static str = "vox"

Source§

impl FileAsset for Image

Source§

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

Source§

impl FileAsset for Obj

Source§

const EXTENSION: &'static str = "obj"

§

impl FileAsset for SharedString

§

const EXTENSION: &'static str = "txt"

§

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

§

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

§

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

§

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