Trait canrun::core::Query

source ·
pub trait Query<'a> {
    // Required method
    fn query<Q: Reify + 'a>(
        self,
        query: Q
    ) -> Box<dyn Iterator<Item = Q::Reified> + 'a>;
}
Expand description

Derive reified values potential resolved states.

Query is implemented for Goals and States, meaning you can call .query() on them with any type that implements Reify.

This is a convenient wrapper around the common pattern of obtaining a StateIter, making sure each resulting state is .ready(), and calling .reify(query) to return only the valid, fully resolved results. Query is implemented on a variety of State related types, allowing it to be used in many contexts.

If a state is not ready (meaning it has open forks and/or constraints still waiting for variables to be resolved) it will not be returned by the query.

A blanket impl covers anything that implements StateIterator, so many types including Goal and State are queryable.

Required Methods§

source

fn query<Q: Reify + 'a>( self, query: Q ) -> Box<dyn Iterator<Item = Q::Reified> + 'a>

Get reified results from things that can produce StateIters.

This will call State::ready() internally, so results will not be returned from states with unresolved constraints.

Examples:
Goals
use canrun::{Query, LVar};
use canrun::goals::unify;

let x = LVar::new();
let goal = unify(x, 1);
let result: Vec<_> = goal.query(x).collect();
assert_eq!(result, vec![1])
State and Option<State>

Most of the lower level State update methods return an Option<State>. Since StateIterator is implemented for this type, Query is as well!

let state: Option<State> = State::new().unify(&x, &Value::new(1));
let result: Vec<_> = state.query(x).collect();
assert_eq!(result, vec![1])

Implementors§

source§

impl<'a, S: StateIterator + 'a> Query<'a> for S