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 = ""
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] = _
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
const HOT_RELOADED: bool = true
If false
, disables hot-reloading of assets of this type (true
by
default).
Required Methods§
Provided Methods§
fn default_value(
id: &SharedString,
error: Box<dyn Error + Sync + Send>,
) -> Result<Self, Box<dyn Error + Sync + Send>>
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.