AssetLocator

Trait AssetLocator 

Source
pub trait AssetLocator<K: AssetKey>:
    Send
    + Sync
    + 'static {
    // Required method
    fn locate(&self, key: &K) -> LocateResult<K::Asset>;
}
Expand description

Trait for locating and loading assets.

Implement this trait to define how assets are found for a given key type. Different locators can be registered for different platforms:

  • Filesystem locator for desktop
  • Network locator for web/playground
  • Memory locator for testing

§Example

struct FileSystemLocator {
    base_path: PathBuf,
}

impl AssetLocator<FilePath> for FileSystemLocator {
    fn locate(&self, key: &FilePath) -> LocateResult<String> {
        // For sync IO, could read directly:
        // let path = self.base_path.join(&key.0);
        // match std::fs::read_to_string(&path) {
        //     Ok(content) => LocateResult::Ready(content),
        //     Err(_) => LocateResult::NotFound,
        // }

        // For async IO, return Pending:
        LocateResult::Pending
    }
}

Required Methods§

Source

fn locate(&self, key: &K) -> LocateResult<K::Asset>

Attempt to locate an asset for the given key.

This method should be fast and non-blocking:

  • Return Ready(value) if the asset is immediately available
  • Return Pending if the asset needs async loading
  • Return NotFound if the asset cannot be found

For assets requiring IO, typically return Pending and let the user fetch the asset externally, then call runtime.resolve_asset().

Implementors§