Derive Macro assets_manager::Asset

source ·
#[derive(Asset)]
{
    // Attributes available to this derive:
    #[asset_format]
}
Available on crate feature macros only.
Expand description

Implements Asset for a type.

Note that the type must implement the right traits for it to work (eg serde::Deserialize or std::str::FromStr).

§Supported formats

§Example

// Define a type loaded as ron
#[derive(Asset, serde::Deserialize)]
#[asset_format = "ron"]
struct Point {
    x: i32,
    y: i32,
}

// Define a type loaded as text
#[derive(Asset)]
#[asset_format = "txt"]
struct Name(String);

impl std::str::FromStr for Name {
    type Err = BoxedError;

    fn from_str(s: &str) -> Result<Self, BoxedError> {
        Ok(Self(String::from(s)))
    }
}

let cache = AssetCache::new("assets")?;

// Load "assets/common/position.ron"
let position = cache.load::<Point>("common.position")?;
assert_eq!(position.read().x, 5);
assert_eq!(position.read().y, -6);

// Load "assets/common/name.txt"
let name = cache.load::<Name>("common.name")?;
assert_eq!(name.read().0, "Aragorn");