pub trait Db {
// Required methods
fn query<Q: Query>(&self, query: Q) -> Result<Arc<Q::Output>, QueryError>;
fn asset<K: AssetKey>(&self, key: K) -> Result<Arc<K::Asset>, QueryError>;
fn asset_state<K: AssetKey>(
&self,
key: K,
) -> Result<AssetLoadingState<K>, QueryError>;
fn list_queries<Q: Query>(&self) -> Vec<Q>;
fn list_asset_keys<K: AssetKey>(&self) -> Vec<K>;
}Expand description
Database trait that provides query execution and asset access.
This trait is implemented by both QueryRuntime and
QueryContext, allowing queries to work with either.
QueryRuntime::query()/QueryRuntime::asset(): No dependency trackingQueryContext::query()/QueryContext::asset(): With dependency tracking
Required Methods§
Sourcefn query<Q: Query>(&self, query: Q) -> Result<Arc<Q::Output>, QueryError>
fn query<Q: Query>(&self, query: Q) -> Result<Arc<Q::Output>, QueryError>
Execute a query, returning the cached result if available.
Sourcefn asset<K: AssetKey>(&self, key: K) -> Result<Arc<K::Asset>, QueryError>
fn asset<K: AssetKey>(&self, key: K) -> Result<Arc<K::Asset>, QueryError>
Access an asset by key.
Returns the asset value if ready, or Err(QueryError::Suspend) if still loading.
Use this with the ? operator for automatic suspension on loading.
§Example
fn query(&self, db: &impl Db) -> Result<MyOutput, QueryError> {
let data = db.asset(key)?; // Suspends if loading
Ok(process(&data))
}Sourcefn asset_state<K: AssetKey>(
&self,
key: K,
) -> Result<AssetLoadingState<K>, QueryError>
fn asset_state<K: AssetKey>( &self, key: K, ) -> Result<AssetLoadingState<K>, QueryError>
Access an asset’s loading state by key.
Unlike asset(), this method returns the full loading state,
allowing you to check if an asset is loading without triggering suspension.
§Example
let state = db.asset_state(key)?;
if state.is_loading() {
// Handle loading case explicitly
} else {
let value = state.get().unwrap();
}Sourcefn list_queries<Q: Query>(&self) -> Vec<Q>
fn list_queries<Q: Query>(&self) -> Vec<Q>
List all executed queries of a specific type.
Sourcefn list_asset_keys<K: AssetKey>(&self) -> Vec<K>
fn list_asset_keys<K: AssetKey>(&self) -> Vec<K>
List all resolved asset keys of a specific type.
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.