AssetLocator

Trait AssetLocator 

Source
pub trait AssetLocator<K: AssetKey>:
    Send
    + Sync
    + 'static {
    // Required method
    fn locate(
        &self,
        db: &impl Db,
        key: &K,
    ) -> Result<LocateResult<K::Asset>, QueryError>;
}
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

§Database Access

The locate method receives a database handle, allowing locators to:

  • Query configuration to determine loading behavior
  • Access other assets as dependencies
  • Make dynamic decisions based on runtime state

Any queries or assets accessed during locate() are tracked as dependencies of the calling query.

§Example

struct ConfigAwareLocator;

impl AssetLocator<FilePath> for ConfigAwareLocator {
    fn locate(&self, db: &impl Db, key: &FilePath) -> Result<LocateResult<String>, QueryError> {
        // Access config to check if path is allowed
        let config = db.query(GetConfig)?.clone();
        if !config.allowed_paths.contains(&key.0) {
            return Err(QueryError::MissingDependency {
                description: format!("Path not allowed: {:?}", key.0),
            });
        }

        // Return pending for async loading
        Ok(LocateResult::Pending)
    }
}

Required Methods§

Source

fn locate( &self, db: &impl Db, key: &K, ) -> Result<LocateResult<K::Asset>, QueryError>

Attempt to locate an asset for the given key.

§Arguments
  • db - Database handle for accessing queries and other assets
  • key - The asset key to locate
§Returns
  • Ok(Ready { value, durability }) - Asset is immediately available
  • Ok(Pending) - Asset needs async loading (will be added to pending list)
  • Err(QueryError) - Location failed (will NOT be added to pending list)
§Dependency Tracking

Any db.query() or db.asset() calls made during this method become dependencies of the query that requested this asset.

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.

Implementors§