pub struct QueryContext<'a, T: Tracer = NoopTracer> { /* private fields */ }Expand description
Context provided to queries during execution.
Use this to access dependencies via query().
Implementations§
Source§impl<'a, T: Tracer> QueryContext<'a, T>
impl<'a, T: Tracer> QueryContext<'a, T>
Sourcepub fn asset<K: AssetKey>(
&self,
key: K,
) -> Result<AssetLoadingState<K>, QueryError>
pub fn asset<K: AssetKey>( &self, key: K, ) -> Result<AssetLoadingState<K>, QueryError>
Access an asset, tracking it as a dependency.
Returns AssetLoadingState<K>:
is_loading()if the asset is still being loadedis_ready()if the asset is available
Use .suspend()? to convert to Result<Arc<K::Asset>, QueryError>,
which returns Err(QueryError::Suspend { asset }) if still loading.
§Example
#[query]
fn process_file(db: &impl Db, path: FilePath) -> Result<Output, QueryError> {
let content = db.asset(path)?.suspend()?;
// Process content...
Ok(output)
}§Errors
Returns Err(QueryError::MissingDependency) if the asset was not found.
Sourcepub fn list_queries<Q: Query>(&self) -> Vec<Q>
pub fn list_queries<Q: Query>(&self) -> Vec<Q>
List all query instances of type Q that have been registered.
This method establishes a dependency on the “set” of queries of type Q. The calling query will be invalidated when:
- A new query of type Q is first executed (added to set)
The calling query will NOT be invalidated when:
- An individual query of type Q has its value change
§Example
#[query]
fn all_results(db: &impl Db) -> Result<Vec<i32>, QueryError> {
let queries = db.list_queries::<MyQuery>();
let mut results = Vec::new();
for q in queries {
results.push(*db.query(q)?);
}
Ok(results)
}Sourcepub fn list_asset_keys<K: AssetKey>(&self) -> Vec<K>
pub fn list_asset_keys<K: AssetKey>(&self) -> Vec<K>
List all asset keys of type K that have been registered.
This method establishes a dependency on the “set” of asset keys of type K. The calling query will be invalidated when:
- A new asset of type K is resolved for the first time (added to set)
- An asset of type K is removed via remove_asset
The calling query will NOT be invalidated when:
- An individual asset’s value changes (use
db.asset()for that)
§Example
#[query]
fn all_configs(db: &impl Db) -> Result<Vec<String>, QueryError> {
let keys = db.list_asset_keys::<ConfigFile>();
let mut contents = Vec::new();
for key in keys {
let content = db.asset(&key)?.suspend()?;
contents.push((*content).clone());
}
Ok(contents)
}