QueryContext

Struct QueryContext 

Source
pub struct QueryContext<'a> { /* private fields */ }
Expand description

Context provided to queries during execution.

Use this to access dependencies via query().

Implementations§

Source§

impl<'a> QueryContext<'a>

Source

pub fn query<Q: Query>( &mut self, query: Q, ) -> Result<Arc<Q::Output>, QueryError>

Query a dependency.

The dependency is automatically tracked for invalidation.

§Example
fn query(&self, ctx: &mut QueryContext) -> Result<Self::Output, QueryError> {
    let dep_result = ctx.query(OtherQuery { id: self.id })?;
    Ok(process(&dep_result))
}
Source

pub fn asset<K: AssetKey>( &mut self, key: &K, ) -> Result<LoadingState<Arc<K::Asset>>, QueryError>

Access an asset, tracking it as a dependency.

Returns LoadingState<Arc<K::Asset>>:

  • LoadingState::Loading if the asset is still being loaded
  • LoadingState::Ready(value) if the asset is available
§Example
#[query]
fn process_file(ctx: &mut QueryContext, path: FilePath) -> Result<Output, QueryError> {
    let content = ctx.asset(&path)?.suspend()?;
    // Process content...
    Ok(output)
}
§Errors

Returns Err(QueryError::MissingDependency) if the asset was not found.

Source

pub fn list_queries<Q: Query>(&mut 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(ctx: &mut QueryContext) -> Result<Vec<i32>, QueryError> {
    let queries = ctx.list_queries::<MyQuery>();
    let mut results = Vec::new();
    for q in queries {
        results.push(*ctx.query(q)?);
    }
    Ok(results)
}
Source

pub fn list_asset_keys<K: AssetKey>(&mut 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 ctx.asset() for that)
§Example
#[query]
fn all_configs(ctx: &mut QueryContext) -> Result<Vec<String>, QueryError> {
    let keys = ctx.list_asset_keys::<ConfigFile>();
    let mut contents = Vec::new();
    for key in keys {
        let content = ctx.asset(&key)?.suspend()?;
        contents.push((*content).clone());
    }
    Ok(contents)
}

Auto Trait Implementations§

§

impl<'a> !Freeze for QueryContext<'a>

§

impl<'a> !RefUnwindSafe for QueryContext<'a>

§

impl<'a> Send for QueryContext<'a>

§

impl<'a> !Sync for QueryContext<'a>

§

impl<'a> Unpin for QueryContext<'a>

§

impl<'a> UnwindSafe for QueryContext<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.